-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
40 lines (36 loc) · 1010 Bytes
/
Copy pathhangman.py
File metadata and controls
40 lines (36 loc) · 1010 Bytes
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
import random
import sys
WORDLIST = [
"apple",
"banana",
"burger",
"equilibrium",
"operator",
"keyboard",
"paragraph",
"obituary",
"technology",
"optimal",
]
HEARTS = 5
word = list(random.choice(WORDLIST)) # e.g. banana
temp = list("_" * len(word)) # e.g. ______
guessedletters = []
while True:
print(" ".join(temp)) # display status (e.g. "b _ n _ n _")
print(f"Guessed letters: {', '.join(guessedletters)}")
if temp == list(word):
print(f"You won with {len(guessedletters)} tries!")
sys.exit()
userinput = input("Guess a letter: ").lower()
if len(userinput) != 1:
print("Only one letter please!")
continue
if userinput in guessedletters:
print("You've already guessed that letter!")
continue
else:
guessedletters.append(userinput)
for i in range(len(word)):
if userinput == word[i]:
temp[i] = word[i]