Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Identify the error type and line number in error messages.
  • Correct syntax errors, name errors, and indentation errors.

How to read errors

A natural part of programming is making mistakes. Even experienced programmers make mistakes when writing code. Errors may result when mistakes are made when writing code. The computer requires very specific instructions telling the computer what to do. If the instructions are not clear, then the computer does not know what to do and gives back an error.

When an error occurs, Python displays a message with the following information:

  1. The line number of the error.
  2. The type of error (Ex: SyntaxError).
  3. Additional details about the error.

Ex: Typing print "Hello!" without parentheses is a syntax error. In Python, parentheses are required to use print. When attempting to run print "Hello!", Python displays the following error:

Traceback (most recent call last):
  File "/home/student/Desktop/example.py", line 1
    print "Hello"
                ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello")?

The caret character (^) shows where Python found the error. Sometimes the error may be located one or two lines before where the caret symbol is shown because Python may not have discovered the error until then. Traceback is a Python report of the location and type of error. The word traceback suggests a programmer trace back in the code to find the error if the error is not seen right away.

Learning to read error messages carefully is an important skill. The amount of technical jargon can be overwhelming at first. But this information can be very helpful.

Checkpoint

Incorrect variable name

Concepts in Practice

Parts of an error

Given the following error message:

Traceback (most recent call last):
  File "/home/student/Desktop/example.py", line 2
    print "test"
        ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("test")?
1.
What is the filename of the program?
  1. Desktop
  2. example.py
  3. test
2.
On which line was the error found?
  1. 1
  2. 2
  3. 3
3.
What type of error was found?
  1. missing parentheses
  2. SyntaxError
  3. traceback

Common types of errors

Different types of errors may occur when running Python programs. When an error occurs, knowing the type of error gives insight about how to correct the error. The following table shows examples of mistakes that anyone could make when programming.

Mistake Error message Explanation

print("Have a nice day!"

SyntaxError: unexpected EOF while parsing
The closing parenthesis is missing. Python is surprised to reach the end of file (EOF) before this line is complete.

word = input("Type a word: )

SyntaxError: EOL while scanning string literal
The closing quote marks are missing. As a result, the string does not terminate before the end of line (EOL).

print("You typed:", wird)

NameError: name 'wird' is not defined

The spelling of word is incorrect. The programmer accidentally typed the wrong key.

prints("You typed:", word)

NameError: name 'prints' is not defined

The spelling of print is incorrect. The programmer accidentally typed an extra letter.

  print("Hello")
IndentationError: unexpected indent
The programmer accidentally typed a space at the start of the line.
    print("Goodbye")
IndentationError: unexpected indent
The programmer accidentally pressed the Tab key at the start of the line.
Table 1.5 Simple mistakes.

Concepts in Practice

Types of errors

For each program below, what type of error will occur?

4.
print("Breakfast options:")
 print("A. Cereal")
 print("B. Eggs")
 print("C. Yogurt")
choice = input("What would you like? ")
  1. IndentationError
  2. NameError
  3. SyntaxError
5.
birth = input("Enter your birth date: )
print("Happy birthday on ", birth)
  1. IndentationError
  2. NameError
  3. SyntaxError
6.
print("Breakfast options:")
print(" A. Cereal")
print(" B. Eggs")
print(" C. Yogurt")
choice = intput("What would you like? ")
  1. IndentationError
  2. NameError
  3. SyntaxError

Try It

Three errors

The following program has three errors.

  • Run the program to find the first error, and correct the corresponding line of code.
  • Then run the program again to find and correct the second error.
  • Keep running and correcting the program until no errors are found.

Try It

Wrong symbols

This code is based on an earlier example, but the code contains several mistakes.

  • One line is missing required punctuation, and another line uses incorrect symbols.
  • Run the program to find the first error, and correct the corresponding line of code.
  • Keep running and correcting the program until no errors are found.
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Mar 15, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.