-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path94_hangman.py
184 lines (157 loc) · 4.4 KB
/
94_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
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
# based on freecodecamp 12 python beginner project: hangman
# [https://www.youtube.com/watch?v=8ext9G7xspg]
# [https://github.com/kying18/hangman]
#
# added difficulty options, modified visuals
from words import words
from modules import treset
import random
import string
treset()
def get_valid_word(words):
word = random.choice(words)
# only get words w/o dash or whitespace
while '-' in word or ' ' in word:
word = random.choice(word)
# return upper() fixed in github
return word.upper()
def hangman():
word = get_valid_word(words)
word_letters = set(word)
alphabet = set(string.ascii_uppercase)
used_letters = set()
lives = int()
difficulty = str()
# no need for dual input() if difficulty is set to str() before while loop
# difficulty = input('Enter difficulty: (E)asy, (D)ynamic, (K) or (S)oulslike? : ')
# while (difficulty != 'E') or (difficulty != 'D') or (difficulty != 'K') or (difficulty != 'S'):
# ^_ way too long, looks like this can be combined in one statement which is really nice
while difficulty != 'E' != 'D' != 'K' != 'S':
difficulty = input('Enter difficulty: (E)asy, (D)ynamic, (K) or (S)oulslike? : ').upper()
if difficulty == 'E':
print(f"({difficulty})asy equals 10 lives")
lives = 10
break
elif difficulty == 'D':
print(f"({difficulty})ynamic lives based on word length")
lives = len(word_letters)
break
elif difficulty == 'S':
print(f"({difficulty})oulslike equals 1 live, happy dying!")
lives = 1
break
elif difficulty == 'K':
print(f"({difficulty})illed yourself !")
lives = 0
break
print('Invalid choice !')
while len(word_letters) > 0 and lives > 0:
# w/o visual
# letters used as string ' '.join(['a', 'b', 'cd']) --> 'a b cd'
# print('Used letters : ', ' '.join(used_letters))
# w/ visual
print('You have', lives, 'lives left and used these letters : ', ' '.join(used_letters))
# current solve status ie (W - R D)
# variable "letter" defined in 'for letter in word' loop at eol
word_list = [letter if letter in used_letters else '-' for letter in word]
# print visuals from dict below with "lives" as "key"
print(lives_visual_dict[lives])
print('Current word : ', ' '.join(word_list))
# get user input
user_letter = input('Guess a letter : ').upper()
if user_letter in alphabet - used_letters:
used_letters.add(user_letter)
if user_letter in word_letters:
word_letters.remove(user_letter)
print('')
else:
lives -= 1
print('Your letter,', user_letter, 'is not in the word\nLives left:',lives)
elif user_letter in used_letters:
print('Already used character ... try again! ')
else:
print('Invalid character! ')
if lives == 0:
print(lives_visual_dict[lives])
print('You died and lost all your souls ! the word was: ',word)
else:
print('Yay, you won, final word : ', ' '.join(word))
# visual representation
lives_visual_dict = {
0: """
_________
| / |
|/ ( )
| |
| / \\
|
""",
1: """
_________
| / |
|/ ( )
| |
| /
|
""",
2: """
_________
| / |
|/ ( )
| |
|
|
""",
3: """
_________
| / |
|/ ( )
|
|
|
""",
4: """
_________
| / |
|/
|
|
|
""",
5: """
_________
| /
|/
|
|
|
""",
6: """
_________
|
|
|
|
|
""",
7: """
|
|
|
|
|
""",
8: """
|
|
|
|
""",
9: """
|
|
|
""",
10: "",
}
hangman()