-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
48 lines (45 loc) · 1.45 KB
/
hangman.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
import random
def getrandomword():
lines = open('words.txt').read().splitlines()
myline =random.choice(lines)
return myline
def printch(a):
for i in range(len(a)):
print("",a[i],end="")
if __name__ == '__main__':
new = 'Y'
while(new=='Y'):
word = getrandomword().upper()
cover = []; incorrect = []
for _ in range(len(word)):
print(" _",end="")
cover.append("_")
print("")
limbs = 5
while(limbs>0):
guess = input("Enter a letter to guess: ").upper()
count = 0
for i in range(len(word)):
if(guess==word[i]):
count = count + 1
cover[i]=guess
if((count==0) and (guess not in incorrect)):
incorrect.append(guess)
print("Sorry",guess,"is not in the word")
limbs = limbs - 1
print("Limbs left: ",limbs)
printch(cover)
print("\nIncorrect guesses:",end="")
printch(incorrect); print("\n")
if("_" not in cover):
print("\nYou win!")
new = input("New game [Y/N] ? ")
print("\n")
break
if(limbs==0):
print("\nYou lose!")
print("The word was:",end="")
printch(word)
print("\n")
new = input("New game [Y/N] ? ").upper()
print("\n")