Day 7 – Hangman

  • Day 7 Goals
  • Approaching Complex Problems
  • Step 1 – Pick a Random Word
  • Step 2 – Replacing Blanks with Guesses
  • Step 3 – Checking of the Player has won
  • Step 4 – Keeping Track of Players Lives
  • Step 5 – Improving the User Experience

Project Hangman – The completed version

import random
import hangman_words
import hangman_art
# TODO-1: - Update the word list to use the 'word_list' from hangman_words.py

lives = 6

# TODO-3: - Import the logo from hangman_art.py and print it at the start of the game.

chosen_word = random.choice(hangman_words.word_list)
print(chosen_word) # this is here to facilitate easy testing - remove it to play the game properly

placeholder = ""
word_length = len(chosen_word)
for position in range(word_length):
    placeholder += "_"
print("Word to guess: " + placeholder)

game_over = False
correct_letters = []

while not game_over:
# TODO-6: - Update the code below to tell the user how many lives they have left.
    print(f"****************************{lives}/6 LIVES LEFT****************************")
    guess = input("Guess a letter: ").lower()
# TODO-4: - If the user has entered a letter they've already guessed, print the letter and let them know.
    if guess in correct_letters:
        print(f"You've already guessed {guess}")


    display = ""

    for letter in chosen_word:
        if letter == guess:
            display += letter
            correct_letters.append(guess)
        elif letter in correct_letters:
            display += letter
        else:
            display += "_"

    print("Word to guess: " + display)

# TODO-5: - If the letter is not in the chosen_word, let them know it's not in the word.
#  e.g. You guessed d, that's not in the word. You lose a life.

    if guess not in chosen_word:
        lives -= 1
        print(f"You guessed {guess}, that's not in the word. You lose a life.")

        if lives == 0:
            game_over = True

# TODO 7: - Update the print statement below to give the user the correct word they were trying to guess.
            print(f"*********************** YOU LOSE - the word was {chosen_word} **********************")

    if "_" not in display:
        game_over = True
        print("****************************YOU WIN****************************")

# TODO-2: - Update the code below to use the stages List from the file hangman_art.py
    print(hangman_art.stages[lives])