-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
44 lines (34 loc) · 1.05 KB
/
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
37
38
39
40
41
42
43
44
from random import choice
def run_game():
word: str = choice(["apple","secret","banana"])
username: str = input("What is your name? >> ")
print(f"Welcome to hangman, {username}!")
#setup
guessed: str = ""
tries: int = 3
while tries > 0:
blanks: int = 0
print("Word: ", end = "")
for char in word:
if char in guessed:
print(char, end="")
else:
print("_", end="")
blanks+=1
print()
if blanks == 0:
print("You got it")
break
guess: str = input("Enter a letter: ")
if guess in guessed:
print(f"You already used: '{guess}''. Please try with another leter")
continue
guessed += guess
if guess not in word:
tries -=1
print(f"Sorry, that was wrong... ({tries} tries remaining) ")
if tries == 0:
print("No more tries remaining... You lose.")
break
if __name__ == '__main__':
run_game()