-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman
More file actions
56 lines (42 loc) · 1.49 KB
/
hangman
File metadata and controls
56 lines (42 loc) · 1.49 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
import random
words = ["jazz", "vitality", "flabbergast", "laptop", "ewe", "weatherman", "clasp", "titter", "mean", "substitute", "fan", "peachy", "lake"]
def choose_word():
return random.choice(words)
def display_word(word, guessed_letters):
display = ''
for letter in word:
if letter in guessed_letters:
display += letter
else:
display += '_'
return display
def hangman():
while True:
word = choose_word()
guessed_letters = []
attempts = 6
print("let's play hangman!")
while True:
print("\nattempts left:", attempts)
display = display_word(word, guessed_letters)
print(display)
if display == word:
print("nice job! you got the word:", word)
break
guess = input("guess a letter: ").lower()
if guess in guessed_letters:
print("hey, you already tried that one!")
continue
guessed_letters.append(guess)
if guess not in word:
attempts -= 1
print("that's not right!")
if attempts == 0:
print("game over! the word was:", word)
break
replay = input("wanna play again? (yes/no): ").lower()
if replay != "yes":
print("thanks for playing! :D")
break
if __name__ == "__main__":
hangman()