CMPT165_CommonPythonErrors.pdf

31 views25 pages
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
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 25 pages and 3 million more documents.

Already have an account? Log in
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
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 25 pages and 3 million more documents.

Already have an account? Log in
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"
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 25 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Notes+

Unlimited access to class notes and textbook notes.

YearlyBest Value
75% OFF
$8 USD/m
Monthly
$30 USD/m
You will be charged $96 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.