Error Messages

1.11. Error Messages#

Quite often while you’re programming you’ll find that you make mistakes and your program won’t run or it does the wrong thing.

An important skill to learn is debuggin\\\*g. This is the process of locating and removing errors from your code. When debugging, it’s very useful to be able to read and \\\*understand the error messages.

x = 5
print(X)

The code above produced the following error message.

Traceback (most recent call last):
    File "/home/main.py", line 2, in <module>
        print(X)
            ^
    NameError: name 'X' is not defined. Did you mean: 'x'?
  • The first line is the start of the error message

  • The next few lines tell you where the error is
    • In this example it tells us the error is in line 2 and comes from the code print(X)

    • Sometimes the error is in the line before i.e. if you can’t see any errors in the specified line, try looking at the line above!

  • The last line in the error message tells you the type of error
    • In this example NameError: name ‘X’ is not defined. Did you mean: ‘x’? tells you that there is a variable that hasn’t been defined. In this case the program can’t find the variable X.

1.11.1. Common Types of Errors#

  • NameError: A variable of the specified name cannot be found.

  • SyntaxError: The structure of the code is invalid.

  • TypeError: An operation is being applied to variables of the wrong type.

  • ValueError: A function or operation has received a value of the wrong type.

Question 1

What type of error does the following code produce?

print(2+7)/3)
  1. NameError

  2. SyntaxError

  3. TypeError

  4. ValueError

Solution

This is an example of a SyntaxError.

This is because the brackets don’t match up. There is only one left ( bracket but there are two ) brackets.

Question 2

What type of error does the following code produce?

Print('Hello!')
  1. NameError

  2. SyntaxError

  3. TypeError

  4. ValueError

Solution

Solution is locked

Question 3

What type of error does the following code produce?

pi = 3.1415926
print('pi to 2dp: {:2.f}'.format(pi))
  1. NameError

  2. SyntaxError

  3. TypeError

  4. ValueError

Solution

Solution is locked

Question 4

What type of error does the following code produce?

print('red' - 'blue')
  1. NameError

  2. SyntaxError

  3. TypeError

  4. ValueError

Solution

Solution is locked

Code challenge: Add Two Numbers

You have been provided with some code.

x = int('Enter a number: ")
y = int('Enter another number:')


print('The sum of your two numbers is: {}'.format(x, y))

The code should take in two numbers from the user, add them together, and then display the result to the user. However, the code has bugs!

Debug this code!

Here are some examples of how the program should work:

Example 1

Enter a number: 32
Enter another number: 64
The sum of your two numbers is: 96.00

Example 2

Enter a number: 1234.5678
Enter another number: 9999.99
The sum of your two numbers is: 11234.56

Example 3

Enter a number: 23
Enter another number: -532.824
The sum of your two numbers is: -509.82
Solution

Solution is locked