Lesson 5.2: Hangman Pt. 1

Today’s Goals

  • Start building Hangman in Python
  • Practice strings, lists, and loops
  • Think about how to represent a word with blanks

Warm-Up Question

  • How would you hide the word PYTHON so the player only sees blanks?
  • Call on a student to explain their idea.

Representing the Word

  • We need to store the word as a string
  • We’ll create a list of blanks (_) to display progress

Example Setup

word = "python"
blanks = ["_"] * len(word)
print(" ".join(blanks))

Class Practice

  • Run the example above
  • How many underscores do you see?
  • What happens if you change the word?

Checking a Guess

word = "python"
blanks = ["_"] * len(word)
guess = input("Guess a letter: ")

for i in range(len(word)):
    if word[i] == guess:
        blanks[i] = guess

print(" ".join(blanks))

Discussion

  • How does the loop check each letter?
  • What happens if the letter appears more than once?

Student Challenge

  • Add a loop so the player can keep guessing until all letters are found
  • Print the word when the player wins

Wrap-Up

  • Hangman uses: strings, lists, loops, and conditionals
  • Next time: we’ll add error handling and make the game harder