-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
119 lines (105 loc) · 2.05 KB
/
hangman.py
File metadata and controls
119 lines (105 loc) · 2.05 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import random
man = [
"""______
| |
| |
|
|
|
|
|
|=========""",
"""______
| |
| |
| \O
|
|
|
|
|=========""",
"""______
| |
| |
| \O
| |
| |
|
|
|=========""",
"""______
| |
| |
| \O
| \/|
| |
|
|
|=========""",
"""______
| |
| |
| \O
| \/|\/
| |
|
|
|=========""",
"""______
| |
| |
| \O
| \/|\/
| |
| _/
|
|=========""",
"""______
| |
| |
| \O
| \/|\/
| |
| _/ \_
|
|========="""
]
def drawLevel(man, word, mistakes, answer, letterCount, tried, score):
print(man[mistakes])
print(' '.join(answer) if letterCount > 0 else ' '.join(answer)+"\n\nCongratulations!!\n The word is correct" )
print( "\nSorry you are DEAD!! x_x \n The word is "+word if mistakes >= 6 else '')
if(tried and letterCount > 0):
print("X : " + ' '.join(tried))
def getInput(tried):
ok = False
while not ok:
letter = str(input('\nTry a letter: ')).lower()
if not(letter in tried):
ok = True
return letter
print("You already tried '"+letter+"'")
play = True
score = 0
while(play):
with open('words.txt') as f:
words = f.readlines()
word = words[random.randrange(0, 80000)]
letterCount = len(word)-1
mistakes = 0
tried = []
rand = random.randrange(1,len(word))
out = [ ( word[i-1] if ( rand == i ) else '_' ) for i in range(1,len(word)) ]
while(letterCount > 0 and mistakes < 6):
letter = word[rand-1] if(letterCount == len(word)-1) else getInput(tried)
tried.append(letter)
for idx,el in enumerate(word):
if el == letter:
out[idx] = el
letterCount -= 1
if letter not in word:
mistakes += 1
drawLevel(man, word, mistakes, out, letterCount, tried, score)
score = score+len(word)*2 if mistakes < 6 else score
ask = input("Score : "+str(score)+"\nAny key to continue, Q to quit : ")
if ask.lower() == 'q':
exit()