2024-08-02: Hangman
This is a tiny project created before.
# import module
import random
from hangman_art import logo, stages
from hangman_words import word_list
# print logo
print(logo)
# create an exit condition
game_over = False
# randomly choose a word from word list, and display it.
chosen_word = random.choice(word_list)
# print(f"Pssst, the randomly chosen word is {chosen_word}")
# create underscore _ in display
display = []
for letter in chosen_word:
display.append("_")
lives = 6
# setup while loop of game
while game_over is False:
# ask player give a guess
guess = input("Guess a letter: ")
# check if the letter has been inputted before
if guess in display:
print(f"You have already guessed a \"{guess}\"")
# insert guess to display
for position in range(len(chosen_word)):
if guess == chosen_word[position]:
display[position] = guess
# guess not in chosen_word, lose a life
if guess not in chosen_word:
lives -= 1
print(f"Letter \"{guess}\" is not in the chosen word. You lose a life!")
# print hangman and left lives
print(" ")
print(f"{' '.join(display)}")
print(stages[lives])
print(f"You have {lives} lives left.")
# exit condition
if "_" not in display:
game_over = True
print("You win!")
if lives == 0:
game_over = True
print("You lose!")