-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
72 lines (57 loc) · 2.01 KB
/
Copy pathhangman.py
File metadata and controls
72 lines (57 loc) · 2.01 KB
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
import random
from collections import Counter
somewords = '''apple banana strewberry 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
flag = 0
try:
while (chances !=0) and flag == 0:
print()
chances -= 1
try:
guess = str(input("Enter a letter to guess : "))
except:
print("Enter only letter")
continue
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("your alreadyt guess that letter")
continue
if guess in word:
k = word.count(guess)
for _ in range(k):
letterGuessed += guess
for char in word:
if char in letterGuessed and (Counter(letterGuessed) != Counter(word)):
correct = 0
print(char, end="")
correct += 1
elif (Counter(letterGuessed) == Counter(word)):
print("The word is : ", end="")
print(word)
flag = 1
print("congratulations, you win!")
break
break
if chances <= 0 and (Counter(letterGuessed) != Counter(word)):
print()
print('You lost! Try again..')
print('The word was {}'.format(word))
except KeyboardInterrupt:
print()
print("Bye! Try Again")
exit()