Lesson 5.4: Debugging Game Bugs

Today’s Goals

  • Learn how to read and fix error messages
  • Practice debugging strategies on Hangman
  • Build confidence in fixing broken code

Warm-Up Question

  • When your code breaks, what’s the first thing you do?
  • (Call on 2–3 students)

Debugging Strategies

  • Read the error message carefully
  • Check the line number Python gives you
  • Trace values with print() statements
  • Test one change at a time

Buggy Hangman Example

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

for i in range(len(word)):
    if word[i] = guess:   # ❌ bug here
        blanks[i] == guess

print(" ".join(blanks))

Class Practice

  • Copy the buggy code
  • What error shows up?
  • How can we fix it?

Another Buggy Example

word = "python"
guess = input("Guess a letter: ")

if guess.isalpha():
    print("Good guess!")
else:
    print("Invalid input!")

if guess in word:
    print("Yes!")
print("You guessed right!")   # ❌ always prints

Discussion

  • Why does the last line always print?
  • How could we fix this?

Student Challenge

  • Fix at least 3 different bugs in provided Hangman snippets
  • For each bug:
    • Write the error message
    • Explain what caused it
    • Show the corrected code

Wrap-Up

  • Debugging is about careful reading and testing
  • Next time: We’ll build full Hangman with groups