-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathDay007-Hangman_Game.py
43 lines (33 loc) · 1.1 KB
/
Day007-Hangman_Game.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
import random
from hangman_art import stages, logo
from hangman_words import word_list
from replit import clear
print(logo)
game_is_finished = False
lives = len(stages) - 1
chosen_word = random.choice(word_list)
word_length = len(chosen_word)
display = []
for _ in range(word_length):
display += "_"
while not game_is_finished:
guess = input("Guess a letter: ").lower()
#Use the clear() function imported from replit to clear the output between guesses.
clear()
if guess in display:
print(f"You've already guessed {guess}")
for position in range(word_length):
letter = chosen_word[position]
if letter == guess:
display[position] = letter
print(f"{' '.join(display)}")
if guess not in chosen_word:
lives -= 1
print(f"You guessed {guess}\nThat's not in the word.\nYou lose a life. {lives} lifes left")
if lives == 0:
game_is_finished = True
print(f"You lose.\nThe word was {chosen_word}")
if not "_" in display:
game_is_finished = True
print("You win.")
print(stages[lives])