Class Notes (1,100,000)
CA (630,000)
SFU (10,000)
CMPT (500)
Lecture

CMPT165_CommonPythonErrors.pdf


Department
Computing Science
Course Code
CMPT 165
Professor
Colin Stewart

This preview shows pages 1-3. to view the full 25 pages of the document.
Common Python Errors
Before being able to determine what your error is and how to fix it, first, what kind of error is it?
There are three main possibilities:
the error occurs when I try to run the program, before the program actually starts. An error
message appears in a little box that says “Syntax Error” – like this:
o this is a syntax error – a problem with the format or spelling of your code
the error occurs while the program is running. An error message appears in red text inside
the Python shell – like this:
Traceback (most recent call last):
File "E:/Program Files/Python25/error.py", line 1, in <module>
print x
NameError: name 'x' is not defined
o this is a runtime error
there is no error message. The program just doesn’t do what I want it to. Or there is an
error message, but only under certain circumstances; other times, the program runs fine.
o this is a logic error
there is an error message in a little box, but it does not say “Syntax Error” – for example –
“I/O Warning” or “Subprocess Startup Error.” Or, random text appears in the Shell after
raw_input() runs in your program.
o this is a special error

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Common Errors
Syntax Errors .............................................................................................................................3
Missing quotation mark................................................................................................................................3
Missing bracket ..............................................................................................................................................4
Missing + between quoted text and a variable..........................................................................................5
Typo (mis-spelling)........................................................................................................................................6
Invalid variable name....................................................................................................................................7
Confusing = and == ....................................................................................................................................8
Indentation problem.....................................................................................................................................9
Escape character error................................................................................................................................10
Missing colon after if/elif/else/for .........................................................................................11
Condition after else.................................................................................................................................12
Runtime Errors........................................................................................................................ 13
Using a variable that doesn’t exist.............................................................................................................13
Mis-spelled variable name ..........................................................................................................................14
Converting something that cannot be converted ...................................................................................16
List index error ............................................................................................................................................17
Logical Errors.......................................................................................................................... 18
x + 1 error ................................................................................................................................................19
Incorrect if/elif/else structure..................................................................................................20
Mis-spelled variable name ..........................................................................................................................22
Special Errors...........................................................................................................................23
Unrecognized character..............................................................................................................................23
Subprocess Startup Error...........................................................................................................................24
Weird text in the Python Shell after raw_input................................................................................25

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Syntax Errors
Missing quotation mark
Error message:
Meaning
You’re missing a quotation mark.
Example:
print "bob
Solution
Add a quotation mark to match the opening quotation mark:
print "bob"
You're Reading a Preview

Unlock to view full version