-
Notifications
You must be signed in to change notification settings - Fork 168
/
HangmanGame.py
73 lines (59 loc) · 1.97 KB
/
HangmanGame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import random
from collections import Counter
someWords = '''apple banana mango strawberry orange grape pineapple apricot lemon coconut watermelon
cherry papaya berry peach lychee muskmelon'''
someWords = someWords.split(' ')
word = random.choice(someWords)
if __name__ == '__main__':
print('Guess the word! HINT: word is a name of a fruit')
for i in word:
print('_', end = ' ')
print()
playing = True
letterGuessed = ''
chances = len(word) + 2
correct = 0
try:
while (chances != 0):
print()
chances -= 1
try:
guess = str(input('Enter a letter to guess: '))
except:
print('Enter only a letter!')
continue
# Validation of the guess
if not guess.isalpha():
print('Enter only a LETTER')
continue
elif len(guess) > 1:
print('Enter only a SINGLE letter')
continue
elif guess in letterGuessed:
print('You have already guessed that letter')
continue
# If letter is guessed correcly
if guess in word:
letterGuessed += guess
# Print the word
for char in word:
if char in letterGuessed:
print(char, end = ' ')
correct += 1
else:
print('_', end = ' ')
# If user has guessed all the letters
if (Counter(letterGuessed) == Counter(word)):
print()
print('Congratulations, You won!')
break
# If user has used all of his chances
if chances == 0:
print()
print('You lost! Try again..')
print('The word was {}'.format(word))
except KeyboardInterrupt:
print()
print('Bye! Try again.')
exit()
# print(letterGuessed)