-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
26 lines (23 loc) · 814 Bytes
/
hangman.py
File metadata and controls
26 lines (23 loc) · 814 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
import random
wordlist = [ "apple", "banana", "burger", "equilibrium", "operator", "keyboard", "paragraph", "obituary", "technology", "optimal" ]
word = list(random.choice(wordlist)) # e.g. banana
temp = list("_"*len(word)) # e.g. ______
guessedletters = []
while True:
print(" ".join(temp))
print(f"Guessed letters: {" ".join(guessedletters)}")
if temp == list(word):
print(f"You won with {len(guessedletters)} tries!")
exit()
userinput = input("Guess a letter: ")
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]