Day 7 - Hangman


Posted by pei_______ on 2022-04-17

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


import random
from replit import clear
from hangman_art import logo , stages
from hangman_words import word_list

print(logo)
chosen_word = random.choice(word_list)
word_length = len(chosen_word)
already_guessed = []
lives = 6

display = []
for _ in range(word_length):
    display += "_"

while "_" in display and lives != 0:
  guess = input("Guess a letter: ").lower()
  clear()
  if guess in already_guessed:
    print(f"You've already guessed {guess}.")
  elif guess not in chosen_word:
    print(f"You guess {guess}, that's not in the word. You lose a life.")
    lives -= 1
  for position in range(word_length):
      letter = chosen_word[position]
      if letter == guess:
        display[position] = letter

  already_guessed += guess
  print(stages[lives])
  print(f"{' '.join(display)}")

if "_" not in display:
  print("\nYou win!")

if lives == 0:
  print("\nYou lose!")

#Python #課堂筆記 #100 Days of Code







Related Posts

[FE302] React 基礎 - hooks 版本 (Function component vs Class component)

[FE302] React 基礎 - hooks 版本 (Function component vs Class component)

Day 8 - 基礎計概 & 演算法

Day 8 - 基礎計概 & 演算法

[FE201] babel

[FE201] babel


Comments