By
now you have probably had at least one of these programs give an error instead
of running smoothly. Your first bug! And maybe your second, and third...
Do
not feel bad. Bugs happen every day, to every programmer. Many of the programs
in this text had errors when they were first being written. Very little
software is written that does not (at least initially) have bugs.
Being
a good programmer does not require that you write perfect code. It requires
that you be detail oriented, so you can locate where you made mistakes and fix
them.
If
your code is not working, there are a number of things you can do.
First,
don’t panic!
Second,
do grumble and complain. Grumbling and complaining is a key part of being a
programmer. (Just a little joke.)
Third,
look over your code very carefully.
There
are a number of ways you can force yourself to be careful. Try saying your code
out loud, line by line. Say each word. If you are copying one of the examples
from this text, try reading both programs backwards. This tricks your brain
into looking at individual words, instead of whole sentences, and can uncover
some subtle mistakes.
Fourth,
memorize the most common types of mistakes. In Python, these are: forgetting a
quotation mark, misspelling a keyword like print, and adding extra indentation
(Python cares a lot about indentation, but we will get to that later).
When
your program has an error, the Python interpreter will often print out an error
message. Error messages are supposed to be useful, but if you are new to
programming, they can be intimidating. We will look at some common errors to
help you understand what Python is saying.
Open
the interactive mode. For each of the following examples, run the code, read
the output, then take a moment to try to determine what is wrong before reading
the answer.
What is wrong with this code?
It
gives the output:
File
"1.2.2.a.py", line 1
print "This is a string of text
^
SyntaxError:
EOL while scanning string literal
The
first two lines of output indicate which line number the Python interpreter
thinks is wrong. It is not always correct, but in this case, that is the line
with the error.
The
last line of output tells us the code has a syntax error. Remember that syntax
is the set of rules to follow when writing code. Python is saying that this
code does not follow those rules.
EOL
is an abbreviation for “End Of Line”. "EOL while scanning string
literal" means Python was trying to read the string, and unexpectedly
reached the end of the line before reaching the end of the string.
That
is because the code is missing a quotation mark at the end of the string.
What is wrong with this code?
It
gives the output:
File
"1.2.2.b.py", line 2
print 3 + 5 +
^
SyntaxError:
invalid syntax
Not
so helpful this time. It is saying something is wrong about the syntax, but it
does not say what. Notice the little arrow pointing to the last plus sign. The
code is "3 + 5 +". This is an incomplete calculation. Python is
looking for something to be on the other side of that plus.
What is wrong with this code?
It
gives the output:
File
"1.2.2.c.py", line 2
pirnt "Cricket wicket"
^
SyntaxError:
invalid syntax
This
time, the output does not say what type of syntax error has occurred, and the
arrow does not point to the right place. But it does still say that the mistake
was on line 2.
Look
back at the highlighted code. The word print is highlighted in
blue, except on line 2. That is because on line 2, it is misspelled as
"pirnt".
In
most code editors, keywords and other language features are highlighted in
various colors. So, you can use color coding to locate errors!
What is wrong with this code?
It
gives the output:
File
"1.2.2.d.py", line 3
print "Third"
^
IndentationError:
unexpected indent
The
Python interpreter is saying that there is a mistake on line 3, and that
mistake is an "IndentationError". Python cares a lot about
indentation; line 3 should not be indented (we will introduce indentation rules
later in this section).
If your program still does not run, remember: don’t panic! With enough patience, every bug is fixed.