This repository was archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
315 lines (251 loc) · 7.61 KB
/
hangman.py
File metadata and controls
315 lines (251 loc) · 7.61 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# from hangman import list_of_letters
# PART 1
# display a menu with at least 3 difficulty choices and ask the user
# to select the desired level
# difficulty = "1" # sample data, normally the user should choose the difficulty
import random
import sys
level = ("1", "2", "3")
lives = 0
act_level = 0
difficulty1 = []
difficulty2 = []
difficulty3 = []
list_of_letters = []
lenght = []
def ask_level():
while True:
level_in = input("Select the desired level! (1,2,3) ")
if valid_level(level_in):
act_level = int(level_in)
return level_in
elif level_in.lower() == str("quit"):
print("Goodbye")
sys.exit(0)
else:
print("Please choose 1,2 or 3! ")
return level_in
def valid_level(level_in):
if level_in in level:
return True
else:
return False
def number_of_lives(level_in):
lives = int(level_in) + 5
return lives
def words_preproc():
f = open(r"C:\Users\dorah\OneDrive\Dokumentumok\hangman-python-DoraHor\countries-and-capitals.txt", "r")
file_content = f.readlines()
for x in file_content:
a = x.split("|")
b = len(a[0])
if b < 11:
difficulty1.append(a[0])
elif 21 > b > 10:
difficulty2.append(a[0])
elif b > 20:
difficulty3.append(a[0])
c = len(a[1])
if c < 11:
difficulty1.append(a[1])
elif 21 > c > 10:
difficulty2.append(a[1])
elif c < 20:
difficulty3.append(a[1])
def random_word(act_level):
word_to_guess = random.choice(difficulty1)
return trim_the_word(word_to_guess)
def trim_the_word(input):
return list(input.strip())
def hangman_core(word_to_guess, already_tried_letters,lives,level_in):
guessed = False
already_tried_letters = set()
word_to_guess_upper = []
for e in word_to_guess:
word_to_guess_upper.append(e.upper())
# print(word_to_guess)
while not guessed and lives > 0:
letters = input("Please give me a letter! ").upper()
if letters == "QUIT":
print("Goodbye")
sys.exit(0)
if letters in already_tried_letters:
print("You already guessed the letter", letters)
elif letters in word_to_guess_upper:
print("Good job,", letters, " is in the word!")
already_tried_letters.add(letters)
else:
print(letters, " is not in the word. ")
lives -= 1
already_tried_letters.add(letters)
# show_the_word(word_to_guess,already_tried_letters)
word_completion = show_the_word(word_to_guess,already_tried_letters)
print('Lives: ', lives)
if lives == 0:
print("MUCH TO LEARN YOU STILL HAVE, MY YOUNG PADAWAN!")
merged_word_to_guess = [''.join(word_to_guess)]
print("THE WORD WAS: ")
print(merged_word_to_guess)
hangman(lives,level_in)
if "_" not in word_completion:
print(" Boom! Nailed it! Congrats! :) ")
break
# print(already_tried_letters)
return already_tried_letters, lives
def show_the_word(word_to_guess, already_tried_letters):
l = []
for x in word_to_guess:
if x.upper() in already_tried_letters:
l.append(x)
else:
l.append('_')
print(''.join(l))
return l
# print the hangman
def hangman(lives,level_in):
if level_in == "1" and lives == 6:
print("""""")
if level_in == "1" and lives == 5 or level_in =="2" and lives == 6 or level_in == "3" and lives == 7:
print("""
________ """)
if level_in == "1" and lives == 4 or level_in =="2" and lives == 5 or level_in == "3" and lives == 6:
print("""
|
|
|
|
|
|________""")
if level_in == "1" and lives == 3 or level_in =="2" and lives == 4 or level_in == "3" and lives == 5:
print("""
--------
|
|
|
|
|
|________""")
if level_in == "1" and lives == 2 or level_in =="2" and lives == 3 or level_in == "3" and lives == 4:
print("""
--------
| 0
|
|
|
|
|________""")
if level_in == "1" and lives == 1:
print("""
--------
| 0
| /|\
| |
|
|
|________""")
if level_in == "2" and lives == 2:
print("""
--------
| 0
| |
| |
|
|
|________""")
if level_in == "2" and lives == 1:
print("""
--------
| 0
| /|\
| |
|
|
|________""")
if level_in == "3" and lives == 3:
print("""
--------
| 0
| |
| |
|
|
|________""")
if level_in == "3" and lives == 2:
print("""
--------
| 0
| /|
| |
|
|
|________""")
if level_in == "3" and lives == 1:
print("""
--------
| 0
| /|\
| |
|
|
|________""")
if lives == 0:
print("""
--------
| 0
| /|\
| |
| / \
|
|
|________""")
return lives, level_in
def main():
username = input("What is your username? ")
if username.lower() == str("quit"):
print("Goodbye")
sys.exit(0)
print("Welcome ", username, "Let's play hangman!:) ")
level_in = ask_level()
lives = number_of_lives(level_in)
print('Lives:', (lives))
words_preproc()
word_to_guess = random_word(level_in)
already_tried_letters = set()
already_tried_letters, lives = hangman_core(word_to_guess, already_tried_letters, lives, level_in)
already_tried_letters = show_the_word(word_to_guess, already_tried_letters)
# lives, level_in = hangman(lives,level_in)
if __name__ == '__main__':
main()
# STEP 2
# based on the chosen difficulty level, set the values
# for the player's lives
# word_to_guess = "Cairo" # sample data, normally the word should be chosen from the countries-and-capitals.txt
# lives = 5 # sample data, normally the lives should be chosen based on the difficulty
# STEP 3
# display the chosen word to guess with all letters replaced by "_"
# for example instead of "Cairo" display "_ _ _ _ _"
# STEP 4
# ask the user to type a letter
# here you should validate if the typed letter is the word
# "quit", "Quit", "QUit", "QUIt", "QUIT", "QuIT"... you get the idea :)
# HINT: use the upper() or lower() built-in Python functions
# STEP 5
# validate if the typed letter is already in the tried letters
# HINT: search on the internet: `python if letter in list`
# If it is not, than append to the tried letters
# If it has already been typed, return to STEP 5. HINT: use a while loop here
# already_tried_letters = [] # this list will contain all the tried letters
# STEP 6
# if the letter is present in the word iterate through all the letters in the variable
# word_to_guess. If that letter is present in the already_tried_letters then display it,
# otherwise display "_".
# if the letter is not present in the word decrease the value in the lives variable
# and display a hangman ASCII art. You can search the Internet for "hangman ASCII art",
# or draw a new beautiful one on your own.
# STEP 7
# check if the variable already_tried_letters already contains all the letters necessary
# to build the value in the variable word_to_guess. If so display a winning message and exit
# the app.
# If you still have letters that are not guessed check if you have a non negative amount of lives
# left. If not print a loosing message and exit the app.
# If neither of the 2 conditions mentioned above go back to STEP 4