-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
172 lines (135 loc) · 4.48 KB
/
hangman.py
File metadata and controls
172 lines (135 loc) · 4.48 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
import random
import json
import string
hanger = ['''
_____
| |
|
|
|
_|_''',
'''
_____
| |
O |
|
|
_|_''',
'''
_____
| |
O |
| |
| |
_|_''',
'''
_____
| |
O |
/| |
| |
_|_''',
'''
_____
| |
O |
/|\ |
| |
_|_''',
'''
_____
| |
O |
/|\ |
| |
/ _|_''',
'''
_____
| |
O |
/|\ |
| |
/ \ _|_''',
'''
☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆
☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆
\O/
~WINNER~ | ~WINNER~
|
/ \
☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆
☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆''']
filename = 'words.txt'
with open(filename, 'r') as f:
word_list = json.load(f)
words = [word.lower() for word in word_list]
def get_random_word(words):
'''secret_word is the set word with letters
guess_word is the blanks last and is modified
guess is user input '''
random_word_index = random.randint(0, len(words) - 1)
secret_word = words[random_word_index]
guess_word = list('_' * len(secret_word))
return guess_word, secret_word
# def input_errors(guess, correct_letters, missed_letters):
# if guess not in string.ascii_lowercase: # the alphabet 'abc...'
# print('Please enter a LETTER')
# elif len(guess) != 1:
# print('Please enter only one letter')
# elif guess in correct_letters or guess in missed_letters:
# print('Please enter a different letter')
def user_guess(secret_word, guess_word, missed_letters, correct_letters, lives, h):
print('Your word is')
print(guess_word)
guess = input('\nPlease guess one letter: ')
# input_errors function. Cannot call function
if guess not in string.ascii_lowercase: # the alphabet 'abc...'
print('Please enter a LETTER')
elif len(guess) != 1:
print('Please enter only one letter')
elif guess in correct_letters or guess in missed_letters:
print('Please enter a different letter')
else:
if guess not in secret_word:
missed_letters.append(guess)
print('Missed letters:')
print(missed_letters)
for missed_letter in missed_letters:
lives -= 1
h += 1
print(hanger[h])
print('\nYou have ' + str(lives) + ' chances left\n')
elif guess in secret_word:
x = secret_word.index(guess)
for x in range(len(secret_word)):
if secret_word[x] == guess:
guess_word[x] = guess
print(guess_word)
correct_letters.append(guess)
print('\nCorrect letters:')
print(correct_letters)
win(guess_word, secret_word, lives)
def win(guess_word, secret_word, lives):
if ''.join(guess_word) == secret_word:
print(hanger[7])
elif lives == 0:
print('You lose')
if ''.join(guess_word) == secret_word or lives == 0:
print('Your word was: ' + secret_word)
play = input('Play again? y/n')
if play == 'y':
init_hangman()
elif play == 'n':
pass
def init_hangman():
missed_letters = []
correct_letters = []
guess_word, secret_word = get_random_word(words)
# print(secret_word)
lives = 6
h = 0
print('Welcome to Hangman!')
print(hanger[h])
while True:
user_guess(secret_word, guess_word, missed_letters, correct_letters, lives, h)
init_hangman()