Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b4e2ea6

Browse files
Hnag man game completed ( avinashkranjan#2750)
1 parent 14d473a commit b4e2ea6

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

‎Hang man game/README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Hangman Game
2+
3+
## Introduction
4+
5+
This Hangman game is a simple Python program that allows players to guess a word letter by letter. It's a great project for beginners to practice programming skills and logic.
6+
7+
## How to Play
8+
9+
1. Run the Python script in your terminal or IDE.
10+
2. The game will choose a random word from a list of fruits.
11+
3. The player's task is to guess the word by entering one letter at a time.
12+
4. The game will display the word with underscores representing the unguessed letters.
13+
5. The player has a limited number of chances to guess the correct word.
14+
6. Enter letters to guess, and the game will provide feedback if the letter is correct or not.
15+
7. If the player guesses the entire word before running out of chances, they win!
16+
17+
## How to Run
18+
19+
1. Clone this repository to your local machine.
20+
2. Open your terminal or IDE and navigate to the project folder.
21+
3. Run the Python script using the following command:

‎Hang man game/hangman.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Python Program to illustrate
2+
# Hangman Game
3+
import random
4+
from collections import Counter
5+
6+
someWords = '''apple banana mango strawberry
7+
orange grape pineapple apricot lemon coconut watermelon
8+
cherry papaya berry peach lychee muskmelon'''
9+
10+
someWords = someWords.split(' ')
11+
# randomly choose a secret word from our "someWords" LIST.
12+
word = random.choice(someWords)
13+
14+
if __name__ == '__main__':
15+
print('Guess the word! HINT: word is a name of a fruit')
16+
17+
for i in word:
18+
# For printing the empty spaces for letters of the word
19+
print('_', end=' ')
20+
print()
21+
22+
playing = True
23+
# list for storing the letters guessed by the player
24+
letterGuessed = ''
25+
chances = len(word) + 2
26+
correct = 0
27+
flag = 0
28+
29+
try:
30+
while (chances != 0) and flag == 0: # flag is updated when the word is correctly guessed
31+
print()
32+
chances -= 1
33+
34+
try:
35+
guess = str(input('Enter a letter to guess: '))
36+
except:
37+
print('Enter only a letter!')
38+
continue
39+
40+
# Validation of the guess
41+
if not guess.isalpha():
42+
print('Enter only a LETTER')
43+
continue
44+
elif len(guess) > 1:
45+
print('Enter only a SINGLE letter')
46+
continue
47+
elif guess in letterGuessed:
48+
print('You have already guessed that letter')
49+
continue
50+
51+
# If letter is guessed correctly
52+
if guess in word:
53+
# k stores the number of times the guessed letter occurs in the word
54+
k = word.count(guess)
55+
for _ in range(k):
56+
letterGuessed += guess # The guess letter is added as many times as it occurs
57+
58+
# Print the word
59+
for char in word:
60+
if char in letterGuessed and (Counter(letterGuessed) != Counter(word)):
61+
print(char, end=' ')
62+
correct += 1
63+
# If user has guessed all the letters
64+
# Once the correct word is guessed fully,
65+
elif (Counter(letterGuessed) == Counter(word)):
66+
# the game ends, even if chances remain
67+
print("The word is:", end=' ')
68+
print(word)
69+
flag = 1
70+
print('Congratulations, You won!')
71+
break # To break out of the for loop
72+
break # To break out of the while loop
73+
else:
74+
print('_', end=' ')
75+
76+
# If user has used all of his chances
77+
if chances <= 0 and (Counter(letterGuessed) != Counter(word)):
78+
print()
79+
print('You lost! Try again..')
80+
print('The word was {}'.format(word))
81+
82+
except KeyboardInterrupt:
83+
print()
84+
print('Bye! Try again.')
85+
exit()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /