-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
244 lines (215 loc) · 5.82 KB
/
hangman.py
File metadata and controls
244 lines (215 loc) · 5.82 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!C:\Users\a2219\AppData\Local\Programs\Python\Python35-32\ python
import random
def game():
again = True
game_number = 1
question_list = ['rhythm', 'buzz', 'sometimes', 'jazz music', 'euphemism', 'scared','university', 'ellen', 'grandmother', 'january', 'independent day']
introduction()
while again:
input_list = []
miss_time = [0]
answer = random_question(question_list)
question_list.remove(answer)
current_list = create_list(answer)
finish = False
while not finish:
menu(current_list, miss_time, game_number)
user_guess = input('Guess a letter: ')
while not input_check(input_list, user_guess) :
user_guess = input('Guess a letter: ')
input_list.append(user_guess)
current_list = guess(user_guess, answer, current_list, miss_time)
finish = check_finish(current_list, miss_time, answer, game_number)
if len(question_list) != 0:
again = play_again_question()
game_number += 1
else:
print('There are no more questions.\n')
again = False
print('Game finished.')
def introduction():
print('\nWelcome to Hangman Game.')
print('Guess the letters in the word. You have six chances to have the wrong guess.')
input('Press Enter to start the game.')
def play_again_question():
answer = input('Do you want to play again?(Y/N): ')
while answer not in ('Y', 'y', 'Yes', 'yes', 'N', 'n', 'No', 'no'):
print('Wrong input')
answer = input('Do you want to play again?(Y/N): ')
if answer in ('Y', 'y', 'Yes', 'yes'):
return True
else:
print('Thank you for your playing!\n')
return False
def menu(current_list, graph_counter, game_number):
print('====================================')
print('Game ' + str(game_number) + '\n')
print_graph(graph_counter[0])
print_list(current_list)
print()
def random_question(question_list):
random_number = random.randrange(0, len(question_list), 1)
question = question_list[random_number]
return question
def guess(user_guess, answer, current_guess, graph_counter):
if user_guess not in answer:
graph_counter[0] += 1
print('There is no \'' + user_guess + '\' in this word.\n')
return current_guess
else:
for iterator in range(len(answer)):
if user_guess == answer[iterator]:
if iterator == 0:
current_guess[iterator] = user_guess.upper()
else:
current_guess[iterator] = user_guess
return current_guess
def print_final_answer(answer):
print(answer[0].upper(), end='')
for iterator in range(len(answer)-1):
print(answer[iterator + 1], end='')
def print_list(list_printed):
print(' ', end='')
for iterator in range(len(list_printed)):
print(list_printed[iterator] + ' ', end='')
print()
def create_list(answer):
empty_list = []
for iterator in range(len(answer)):
if answer[iterator] == ' ':
empty_list.append(' ')
else:
empty_list.append('_')
return empty_list
def input_list_check(input_list, user_guess):
repeat_guess = False
iterator = 0
if len(input_list) != 0:
while not repeat_guess and iterator < len(input_list):
if user_guess == input_list[iterator]:
repeat_guess = True
else:
iterator += 1
return repeat_guess
def check_finish(current_list, miss_time, answer, game_number):
finish_switch = True
iterator=0
if miss_time[0] == 6:
menu(current_list, miss_time, game_number)
print('You are dead.\n')
print('The word is actually \'', end='')
print_final_answer(answer)
print('\'.\n')
else:
while finish_switch and iterator < len(current_list):
if current_list[iterator] == '_':
finish_switch = False
else:
iterator += 1
if finish_switch:
menu(current_list, miss_time, game_number)
print('Congratulations! You solved the word!\n')
return finish_switch
def input_check(input_list, user_guess):
if user_guess.isalpha():
if len(user_guess) == 1:
if input_list_check(input_list, user_guess):
print('Repeated guess.\n')
return False
else:
return True
else:
print('Too many letters.\n')
return False
else:
print('Wong input.\n')
return False
def print_graph(miss_num):
graphic = [
"""\
+------+
|
|
|
|
|
==================
|| ||
|| ||
"""
,
"""\
+------+
| |
| O
|
|
|
==================
|| ||
|| ||
"""
,
"""\
+------+
| |
| O
| |
|
|
==================
|| ||
|| ||
"""
,
"""\
+------+
| |
| O
| /|
|
|
==================
|| ||
|| ||
"""
,
"""\
+------+
| |
| O
| /|\\
|
|
==================
|| ||
|| ||
"""
,
"""\
+------+
| |
| O
| /|\\
| /
|
==================
|| ||
|| ||
"""
,
"""\
+------+
| |
| O
| /|\\
| / \\
|
==================
|| ||
|| ||
"""
]
print(graphic[miss_num])
if __name__ == '__main__':
game()