Skip to main content
Engineering LibreTexts

1.6: Error Messages

  • Page ID
    117518
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

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

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

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

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    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 \(\PageIndex{1}\): 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")?

    What is the filename of the program?

    1. Desktop
    2. example.py
    3. test
    Answer

    b. The filename can be found after the last slash (/). Other names, like Desktop, refer to folders.

    Concepts in Practice \(\PageIndex{2}\): 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")?

    On which line was the error found?

    1. 1
    2. 2
    3. 3
    Answer

    b. Line 2 is mentioned immediately after the filename.

    Concepts in Practice \(\PageIndex{3}\): 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")?

    What type of error was found?

    1. missing parentheses
    2. SyntaxError
    3. traceback
    Answer

    b. The type of error is always the first word on the last line of the error message.

    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.

    Table 1.5 Simple mistakes.
    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.
    Concepts in Practice \(\PageIndex{4}\): Types of errors

    For this program, what type of error will occur?

    print("Breakfast options:")
     print("A. Cereal")
     print("B. Eggs")
     print("C. Yogurt")
    choice = input("What would you like? ")
    1. IndentationError
    2. NameError
    3. SyntaxError
    Answer

    a. The middle three lines are indented. In Python, lines may not begin with spaces or tabs unexpectedly.

    Concepts in Practice \(\PageIndex{5}\): Types of errors

    For this program, what type of error will occur?

    birth = input("Enter your birth date: )
    print("Happy birthday on ", birth)
    1. IndentationError
    2. NameError
    3. SyntaxError
    Answer

    c. The quote marks are missing at the end of the input prompt.

    Concepts in Practice \(\PageIndex{6}\): Types of errors

    For this program, what type of error will occur?

    print("Breakfast options:")
    print(" A. Cereal")
    print(" B. Eggs")
    print(" C. Yogurt")
    choice = intput("What would you like? ")
    1. IndentationError
    2. NameError
    3. SyntaxError
    Answer

    b. The name for "input" is misspelled.

    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.
    Interactive Code
    word = input("What is your favorite word? ')
    print()
    print("You said:", Word)
     print("That's all, Folks!")
    
     
    Answer

    word = input("What is your favorite word? ")
    print()
    print("You said:", word)
    print("That's all, Folks!")

    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.
    Interactive Code
    team1 = "Liverpool"
    score1 = "4"
    team2 = "Chelsea"
    score2 = "3"
    
    print(team1 "versus" team2)
    print["Final score:" score1 "to" score2]
    
     
    Answer

    team1 = "Liverpool"
    score1 = "4"
    team2 = "Chelsea"
    score2 = "3"

    print(team1, "versus", team2)
    print("Final score:", score1, "to", score2)


    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.