10. Reading error messages — Homework solutions

The .py solution files are in exercises/10/homework/solutions/. These problems practise the loop: run, read the first error, fix one thing, run again.

Problem 1 — Name the error

Problem. Identify the error type, then fix it.

How to think about it. Run the file, read the message, and match it to one of the four types: unterminated string literal, NameError, IndentationError, or missing colon.

Worked solution. The starter line is:

print("Hello, " + player_name)

with no player_name defined. The error is NameError: name 'player_name' is not defined — the variable has no value. A fix:

player_name = "Keiko"
print("Hello, " + player_name)
# error type: NameError

Problem 2 — Fix the quote

Problem. Repair an unterminated string literal.

Worked solution.

print("A wizard is never late.")

The only change is the missing closing " before the ).

Common mistakes.

  • Adding the quote after the ). The closing quote goes at the end of the text, before the closing parenthesis.

Problem 3 — Fix the typo

Problem. A misspelled function name causes a NameError.

Worked solution. The starter has prnt("Fixed!"). The message names the culprit: name 'prnt' is not defined. Fix the spelling:

print("Fixed!")

Common mistakes.

  • "Fixing" it by inventing a different name. Python only knows the names it knows. When the message says 'prnt', the fix is almost always the real function spelled correctly — print.

Challenge — Three in a row

Problem. Three mistakes, one of each kind, fixed one at a time.

How to think about it. Run. The first error is whichever comes first in the file. Fix it. Run again — the next error shows. Fix it. Run again for the last one. The loop, working as intended.

Worked solution.

print("Starting up")
print("Working")
print("done")

The starter had a missing closing quote on line one, prnt on line two, and an unexpected indent on line three. Fixed in three passes, it prints:

Starting up
Working
done

Common mistakes.

  • Fixing all three at once and losing track of which change did what. One error, one fix, run again — even when you think you see them all.

Done?

That is the end of Part 2. You can install and run Python, print exactly what you want, comment your code, and read Python's errors. The Part 2 mini-project — the ASCII Name Banner — puts your print skills to work building a picture out of text. Then Part 3 digs into variables, strings, numbers, and input.