-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.TheHangman.py
More file actions
33 lines (24 loc) · 771 Bytes
/
Copy path10.TheHangman.py
File metadata and controls
33 lines (24 loc) · 771 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
import random
words=["apple","banana","cherry"]
word_to_guess= random.choice(words)
guesses=["_"] * len(word_to_guess)
attempts = 6
def hangman():
global attempts
while"_" in guesses and attempts >0:
print("\n ".join(guesses))
guess = input("Guess a letter: ").lower()
if guess in word_to_guess:
for i,letter in enumerate(word_to_guess):
if letter == guess:
guesses[i] = guess
print(f"Good Guess!")
else:
attempts -= 1
print(f"Wrong guess. Attempts left: {attempts}")
if "_" not in guesses:
print("\n" + " ".join(guesses))
print("You won!")
else:
print(f"You lost.The word was {word_to_guess}.")
hangman()