-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHangman.py
83 lines (72 loc) · 2.15 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
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
74
75
76
77
78
79
80
81
82
83
# TMW
from random import * # a library that helps in selecting a random number or string
print( 'Hangman ')
words= 'ant bat cat rat mat are orb new net not'.split()
def andword():
a = randint(0,len(words)-1)
return words[a]
secretword= andword()
length= len(secretword)
blanks= '_'
blanks= blanks*length
incorrect_choices=0
ctr=0
correct=''
incorrect=''
def checkguess(g,blanks,ctr,incorrect,correct,incorrect_choices):
for i in secretword:
if g in correct or g in incorrect:
print('You have tried this letter earlier')
break
elif g==i:
print('Correct Guess')
correct=correct+g
blanks= blanks[:ctr]+g+blanks[ctr+1:]
break
ctr=ctr+1
if g not in secretword and g not in incorrect:
print('Incorrect Guess')
incorrect=incorrect+g
incorrect_choices=incorrect_choices+1
print(HANGMAN[incorrect_choices])
return (blanks,incorrect,correct,incorrect_choices)
HANGMAN= [''' ''' #a list of hangman pics
,'''
0 ''','''
0
| ''','''
0
/| ''','''
0
/|\ ''','''
0
/|\
/ ''','''
0
/|\
/ \ ''']
blank='_'
while True:
if '_' in blanks:
g=input('Enter a guess ')
(blanks,incorrect,correct,incorrect_choices)=checkguess(g,blanks,ctr,incorrect,correct,incorrect_choices)
print(blanks)
if incorrect_choices>5 or blanks== secretword:
if incorrect_choices>5:
print('Game Over..... The word was...', secretword)
else:
print('Awesome, you did it!!!!')
print('Do you wanna try again? (y or n)')
ans=input()
if ans=='y':
print ("\n" * 50)
secretword= andword()
length= len(secretword)
blanks= '_'
blanks= blanks*length
incorrect_choices=0
ctr=0
correct=''
incorrect=''
else:
break