Skip to content

Commit f49177a

Browse files
authored
Merge pull request #71 from payal033/Hangman-Game
Hangman Game using Python
2 parents 96fc877 + ec4a76a commit f49177a

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed
1019 Bytes
Binary file not shown.
422 Bytes
Binary file not shown.

Hangman/hangman.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from hangman_art import logo , stages
2+
from hangman_words import wordlist
3+
import random
4+
5+
# get random word from hangman_words
6+
random_word = random.choice(wordlist)
7+
word_len = len(random_word)
8+
9+
print(logo)
10+
print('This is your word.Try to guess it!!')
11+
print('Word : ',end =" ")
12+
13+
guess_word = ['_'] * word_len
14+
print(*guess_word)
15+
16+
chances = 6
17+
end_of_game = False
18+
19+
# game logic
20+
while not end_of_game and chances != 0:
21+
22+
flag = 0
23+
letter = input('\n\nGuess a letter: ').lower()
24+
25+
if (letter.isdigit() or len(letter) != 1):
26+
print('Please enter single letter (Numbers not allowed)')
27+
continue
28+
29+
if letter in guess_word:
30+
print(f"You've already guessed {letter}")
31+
32+
for i in range(word_len):
33+
if random_word[i] == letter:
34+
guess_word[i] = letter
35+
flag = 1
36+
37+
if '_' not in guess_word:
38+
end_of_game = True
39+
print('\nCongo!! You won. :)')
40+
break
41+
42+
if flag == 0:
43+
print(f"You guessed {letter}, that's not in the word. You lose a life")
44+
chances -= 1
45+
46+
print('Guessed Word : ',end =" ")
47+
print(*guess_word)
48+
print(stages[6-chances])
49+
50+
else:
51+
print('\nYou lost :(\nCorrect word:',random_word)
52+
53+
54+
55+

Hangman/hangman_art.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
logo = '''
2+
_
3+
| |
4+
| |__ __ _ _ __ __ _ _ __ ___ __ _ _ __
5+
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
6+
| | | | (_| | | | | (_| | | | | | | (_| | | | |
7+
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
8+
__/ |
9+
|___/
10+
'''
11+
12+
stages = ['''
13+
+---+
14+
| |
15+
|
16+
|
17+
|
18+
|
19+
=========''', '''
20+
+---+
21+
| |
22+
O |
23+
|
24+
|
25+
|
26+
=========''', '''
27+
+---+
28+
| |
29+
O |
30+
| |
31+
|
32+
|
33+
=========''', '''
34+
+---+
35+
| |
36+
O |
37+
/| |
38+
|
39+
|
40+
=========''', '''
41+
+---+
42+
| |
43+
O |
44+
/|\ |
45+
|
46+
|
47+
=========''', '''
48+
+---+
49+
| |
50+
O |
51+
/|\ |
52+
/ |
53+
|
54+
=========''', '''
55+
+---+
56+
| |
57+
O |
58+
/|\ |
59+
/ \ |
60+
|
61+
=========''']

Hangman/hangman_words.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
wordlist = [
2+
'kangaroo',
3+
'basketball',
4+
'pinch',
5+
'pillow',
6+
'elephant',
7+
'prayer',
8+
'telephone',
9+
'hopscotch',
10+
'scissors',
11+
'alligator',
12+
'inchworm',
13+
'snowman',
14+
'walkway',
15+
'wristwatch',
16+
'youthful',
17+
'twilight',
18+
'fisherman',
19+
'baboon']

0 commit comments

Comments
 (0)