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

深入學習 lsd-slam - 5

深入學習 lsd-slam - 5

在Gatsby GraphQL中組合出完美資料

在Gatsby GraphQL中組合出完美資料

D1_Git

D1_Git


Comments