What Are Errors?

Understand runtime errors and see what happens when the quiz crashes on bad input

Your quiz works — as long as the player types a number. Type abc instead, and Python crashes:

Traceback (most recent call last):
  File "quiz.py", line 11, in ask_question
    answer = int(answer_text)
ValueError: invalid literal for int() with base 10: 'abc'

This is a runtime error: valid Python code that fails during execution. The int() call cannot convert "abc" to a number, so Python raises a ValueError and the quiz stops.

Two kinds of errors

  • Syntax errors: Python cannot parse the code. The program does not start at all. A missing colon after def is a syntax error.
  • Runtime errors (exceptions): The code is valid Python, but something fails while it runs. Calling int("abc") is a runtime error.

You cannot catch syntax errors — fix them before running. You *can* catch runtime errors using try/except.

Reading a traceback

Python prints a traceback whenever an exception is not caught:

Traceback (most recent call last):
  File "quiz.py", line 11, in ask_question
    answer = int(answer_text)
ValueError: invalid literal for int() with base 10: 'abc'

Read it from the bottom up:

  • Last line — the exception type and message (ValueError: invalid literal...)
  • Line above — the code that raised it (answer = int(answer_text))
  • Line above that — the file, line number, and function (quiz.py, line 11, ask_question)

What you will fix

In the next chapters, you will wrap int(answer_text) in a try/except block. Invalid input will print a message instead of crashing. Then you will add a retry loop so the quiz keeps asking until the player enters a valid number.

Next Chapter →