-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
58 lines (47 loc) · 1.01 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
import random
f=open('words.txt')
a='word'
ls=[]
while a!='':
a=f.readline()
ls.append(a[:-2])
print "ok ready let's begin"
def printstrikes(n):
print '\n'
striked=n*' x '
chance=(7-n)*' _ '
print striked+chance
def printfield(word,guessed):
field=''
for x in word:
if x in guessed:
field+=' '+x+' '
else: field+=' _ '
if '_' in field:
print field
else:
print "You win!"
return 'done'
playing=True
while playing:
word=random.choice(ls)
strike=0
guessed=''
done='no'
while strike < 7 and done!='done':
print '\n'
print "strikes:"
printstrikes(strike)
print '\n'
print '\n'
done=printfield(word,guessed)
g=raw_input("guess a letter... ")
if g in word:
guessed+=g
else:
strike+=1
if strike==7:
print "You lose"
end=raw_input('play again?, y/n ')
if end=='n':
playing=False