Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

1.6: Error Messages

( \newcommand{\kernel}{\mathrm{null}\,}\)

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
  • example.py
  • 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
  • NameError
  • 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.

    This page titled 1.6: Error Messages is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    Support Center

    How can we help?