Run Time Errors

Line numbers

As with syntax errors, the error message will give you a line number or highlight a point on your code. You should debug the line based on what you are doing on that line.

Common errors

Some common causes of run-time errors are:

Using a variable before it has been initialised

In this example, we try to assign to the variable x based on the variable y, but the variable y has not yet been given a value.

1    x = y + 3

Traceback (most recent call last):
  File "tester.py", line 1, in ?
    x = y + 3
NameError: name 'y' is not defined

This kind of error can also occur when you try to use a variable where you mean to use a literal for example:

1    print hello

Traceback (most recent call last):
  File "tester.py", line 1, in ?
    print hello
NameError: name 'hello' is not defined

What you probably mean here is:

1    print "hello"

which runs without errors and prints "hello".

Misspellings of variable names

If you have mistakenly mistyped a variable name, this will produce an error. Don't forget that Python is case sensitive, so the variable "var" is not the same as the variable "Var"!

In this example, we want to print the variable "greeting", but instead have typed "greetign":

1    greeting = "hello world"
2    print greetign

Traceback (most recent call last):
  File "tester.py", line 2, in ?
    print greetign
NameError: name 'greetign' is not defined

In this example, we have used the wrong case for the variable "Counter" on line 4:

1    counter=0
2    while counter < 5:
3        print "hello"
4        counter = Counter + 1

Traceback (most recent call last):
  File "./tester.py", line 6, in ?
    counter = Counter + 1
NameError: name 'Counter' is not defined

Using the wrong types

Each Python operator can only work on certain variable types. If these are not correct, there will be errors.

In this example, we are attempting to use the string concatenate operator "+" but we are applying it to a string and an integer:

1    numstring = "hello" + 4

Traceback (most recent call last):
  File "./tester.py", line 1, in ?
    numstring = "hello" + 4
TypeError: cannot concatenate 'str' and 'int' objects

If necessary, we can convert an integer into a string:

1    numstring = "hello" + str(4)

Similarly, if we use raw_input() to read a number from the user it will be read as a string and must be converted to an integer. In this example, we are trying to read a number from the user and print that number added to 3.

1    num = raw_input()
2    print num + 3

Traceback (most recent call last):
  File "tester.py", line 2, in ?
    print num + 3
TypeError: cannot concatenate 'str' and 'int' objects

Note that in this case, Python sees that the first item used by the "+" operator (num) is a string variable, so it assumes we mean the "+" to concatenate and gives us a concatenation error.

We can fix the program by converting the user input into an integer:

1    num = raw_input()
2    print int(num) + 3
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License