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
SyntaxError: '(' was never closed— what was left unfinished?print("See you later!"print(Hello)crashes withNameError: name 'Hello' is not defined. Why does Python treatHelloas a name?age = int("ten")stops withValueError: invalid literal for int() with base 10: 'ten'. Translate that into English, then fix.IndentationError: expected an indented block— expected where?if score > 90: print("Amazing!")ZeroDivisionError: division by zero— but nobody typed a zero.people = 0 print("Slices each:", 8 / people)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
Answer 1
The bracket. Every
(needs a)— add it after the quote and the line printsSee you later!exactly once.
Answer 2
Without quotes,
Hellolooks like a variable — a name — and no variable calledHelloexists. Quote it:print("Hello").
Answer 3
“You asked me to turn
'ten'into a whole number, and I cannot.”int()reads digits, not words —int("10")works fine.
Answer 4
Indented under the
if. The line after a:must step right so Python knows it belongs to the condition. Indent the
Answer 5
The zero arrived by variable —
peopleholds0, so line 2 divides by zero. Givepeoplea sensible value, or check before dividing.
Answer 6
The
whileline needs a colon at the end. Fixed, it runs — forever: nothing inside the loop ever changeslives. Crashes announce themselves; logic bugs like this one quietly ruin your day.
Curriculum connection
C2.6
interpret program errors and implement strategies to resolve them
Link to original