-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
41 lines (39 loc) · 1.24 KB
/
Copy pathhangman.py
File metadata and controls
41 lines (39 loc) · 1.24 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
import random
def hangman():
word_list = ["The", "Self", "Taught", "Programmer"]
random_number = random.randint(0, 3)
word = word_list[random_number]
wrong_guesses = 0
stages = ["",
"________ ",
"| ",
"| | ",
"| 0 ",
"| /|\ ",
"| / \ ",
"| "
]
remaining_letters = list(word)
board = ["__"] * len(word)
win = False
print('Welcome to Hangman!')
while wrong_guesses < len(stages) - 1:
print('\n')
guess = input("Guess a letter: ")
if guess in remaining_letters:
ch_index = remaining_letters.index(guess)
board[ch_index] = guess
remaining_letters[ch_index] = '$'
else:
wrong_guesses += 1
print((' '.join(board)))
print('\n'.join(stages[0: wrong_guesses + 1]))
if '__' not in board:
print('You win! The word was:')
print(' '.join(board))
win = True
break
if not win:
print('\n'.join(stages[0: wrong_guesses]))
print('You lose! The words was {}'.format(word))
hangman()