-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
36 lines (28 loc) · 853 Bytes
/
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
from random import choice
with open('word_list.txt', 'r') as file:
words = file.readlines()
secret_word = choice(words)[:-1]
allowed_guesses = 8
guesses = []
game_over = False
while not game_over:
for char in secret_word:
if char.lower() in guesses:
print(char, end=' ')
else:
print('_', end=' ')
print('')
guess = str(input(f'Guesses left: {allowed_guesses}\nGuess: '))
guesses.append(guess.lower())
if guess.lower() not in secret_word.lower():
allowed_guesses -= 1
if allowed_guesses == 0:
break
game_over = True
for letter in secret_word:
if letter.lower() not in guesses:
game_over = False
if game_over:
print(f'You guessed it, the word was {secret_word}!')
else:
print(f'Game over, the word was {secret_word}!')