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

net use 網路磁碟機指令

net use 網路磁碟機指令

CSS保健室|background-blend-mode

CSS保健室|background-blend-mode

2021 年末幾本有印象的書

2021 年末幾本有印象的書


Comments