Six snippets, six crashes. For each: read the error message first and say what Python is complaining about, then fix the code, then predict what the fix does. Debugging Step by Step has the method.

Questions

  1. SyntaxError: '(' was never closed — what was left unfinished?
    print("See you later!"
  2. print(Hello) crashes with NameError: name 'Hello' is not defined. Why does Python treat Hello as a name?
  3. age = int("ten") stops with ValueError: invalid literal for int() with base 10: 'ten'. Translate that into English, then fix.
  4. IndentationError: expected an indented block — expected where?
    if score > 90:
    print("Amazing!")
  5. ZeroDivisionError: division by zero — but nobody typed a zero.
    people = 0
    print("Slices each:", 8 / people)
  6. SyntaxError: expected ':' — fix it, then look again: a second bug is hiding here. Predict what the fixed code does.
    while lives > 0
        print("Still playing")

Answers

Curriculum connection

C2.6

interpret program errors and implement strategies to resolve them

Link to original