Pin It

PYTHON ERROR MESSAGES

If you are a beginner in Python you will run into error messages quite often. In this webpage we will be listing python error messages with the respective cause of the error. We are testing code using Atom text editor and viewing the codes output using Windows command prompt screen (on a Windows 10 PC).

QUICK SEARCH

For example, if you run into an error code that says 'IndexError: string index out of range', copy and paste the error description in the search box just above the table to quickly locate the error within our table. 

PYTHON CODE

PYTHON ERROR

ERROR CAUSE

name = "Joe"
age = 24
print(name + " is " + age + " years old")

C:\Users\User\Desktop\PROJECT PYTHON>index.py
Traceback (most recent call last):
File "C:\Users\User\Desktop\PROJECT PYTHON\index.py", line 16, in
print(name + " is " + age + " old")
TypeError: can only concatenate str (not "int") to str
C:\Users\User\Desktop\PROJECT PYTHON>

Cannot concatenate string variables with integer variables

var1 = 'abc'
    print(var1[3])

C:\Users\User\Desktop\PROJECT PYTHON>index.py
Traceback (most recent call last):
File "C:\Users\User\Desktop\PROJECT PYTHON\index.py", line 11, in
print(var1[5])
IndexError: string index out of range
C:\Users\User\Desktop\PROJECT PYTHON>

If you are searching within a string for a non-existing value (a value that is larger than the string length) you will get this error message.

variable name = 1

print(variable name)

C:\Users\User\Desktop\PROJECT PYTHON>index.py
File "C:\Users\User\Desktop\PROJECT PYTHON\index.py", line 13
var name = 1
^
SyntaxError: invalid syntax

C:\Users\User\Desktop\PROJECT PYTHON>

A variable name cannot contain a space.

userInput = input("what is your speed\n")
print("The speed you entered is ",int(userInput))

C:\Users\User\Desktop\PROJECT PYTHON>index.py
what is your speed
5.3
Traceback (most recent call last):
File "C:\Users\User\Desktop\PROJECT PYTHON\index.py", line 22, in
print("The speed you entered is ",int(userInput))
ValueError: invalid literal for int() with base 10: '5.3'

C:\Users\User\Desktop\PROJECT PYTHON>

This code works good only if the user enters a whole number. If user enters a floating point number (decimal) then the error code will display.

SOLUTION: Change part of the code - ("The speed you entered is ",int(userInput)) - with this - ("The speed you entered is ",float(userInput))

print "round(37.55869855, 2) : " , round(37.55869855, 2)

C:\Users\User\Desktop\PROJECT PYTHON>index.py
File "C:\Users\User\Desktop\PROJECT PYTHON\index.py", line 27
print "round(37.55869855, 2) : " , round(37.55869855, 2)
^
SyntaxError: invalid syntax

C:\Users\User\Desktop\PROJECT PYTHON>

This error is caused by print function missing brackets;

print"round(37.55869855, 2) : " , round(37.55869855, 2)

CORRECTION:

print("round(37.55869855, 2) : " , round(37.55869855, 2))

var1 = None;
print(none)

C:\Users\User\Desktop\PROJECT PYTHON>index.py
Traceback (most recent call last):
File "C:\Users\User\Desktop\PROJECT PYTHON\index.py", line 25, in
print(none)
NameError: name 'none' is not defined

C:\Users\User\Desktop\PROJECT PYTHON>

Variable name is not typed correctly

CORRECTION:

var1 = None;
print(None)

x = 10
print(x > 0 and < 10)

C:\Users\User\Desktop\PROJECT PYTHON>index.py
File "C:\Users\User\Desktop\PROJECT PYTHON\index.py", line 16
print(x > 0 and < 10)
^
SyntaxError: invalid syntax

C:\Users\User\Desktop\PROJECT PYTHON>

The x is missing after the 'and' logical operator;

SOLUTION:

x = 10
print(x > 0 and x < 10)

if x > 0:
print('x is positive')

C:\Users\User\Desktop\PROJECT PYTHON>index.py
Traceback (most recent call last):
File "C:\Users\User\Desktop\PROJECT PYTHON\index.py", line 12, in
if x > 0:
NameError: name 'x' is not defined

C:\Users\User\Desktop\PROJECT PYTHON>

The variable x has not been defined.

SOLUTION:

x = 10
if x > 0:
print('x is positive')

OUTPUT:

C:\Users\User\Desktop\PROJECT PYTHON>index.py
x is positive

C:\Users\User\Desktop\PROJECT PYTHON>