silent_error.py
Final total is 9
Reading: Think Python Appendix A
Now it is our turn to debug Python code. Perhaps it will be wise to approach the subject cautiously.
Error messages exist to help us. In general, they tell us that some of the code we’ve written cannot be understood by Python. Knowing how to read error messages is important.
Some errors are very easy to read:
EOL while scanning string literal
tells us that Python was reading a string
'
or "
or """
^
tells us where in the code the error was foundNameError name y is not defined
tells us that we have tried to use variable y
before assigning anything to itnot subscriptable
, it means we have tried to index something that can’t be indexed
x[2]
refers to the 3rd element of x
- if x
is a list, string, or tuplex
in this example is an int, the “3rd element” can’t be interpreted logically[
]
to pick out an element of a collectionnot callable
means we tried to call something as if it were a function
x
is not a function, so we can’t call it(
and )
instead of square brackets [
]
SyntaxError: invalid syntax
^
tells us a lot!
{
All of these error examples have been from the interpreter, but when we get error messages from running a Python program written in the editor, we get an extra detail: line numbers
Here’s a program with an error:
)
is on line 3Sometimes we write a program that has no errors, but the program does not do what we intended.
silent_error.py
Final total is 9
\(3 + 1 + 0 + 2 + 3 + 1 = 10\)
What happened here?
We could use the visualizer to inspect the code, but we can also add print statements to see what our program is doing:
silent_error.py
Total is 0 || j is 3 || adding 2
Total is 2 || j is 1 || adding 1
Total is 3 || j is 0 || adding 3
Total is 6 || j is 2 || adding 0
Total is 6 || j is 3 || adding 2
Total is 8 || j is 1 || adding 1
Final total is 9
We weren’t adding the quantity in to the total we intended to add!
for j in A
loops through the values in A directly, not the indicesA
based on the values!Here it is in the visualizer:
The visualizer is a valuable tool, but print statements are often more useful for longer programs.
error_trace.py
y == -1 * x
is a comparison=
: y = -1 * x
absolute_value
functionabsolute_value
function is called on line 12 with no error
A
contains no negativesabsolute_value
is called on line 14
B
contains a negativeabsolute_value
has a negative argumentAdd print statements to error_trace.py
to find the value of C before the error happens, and the value of the argument passed to absolute_value
when the error happens.
Running this program results in an error. Read the error message and figure out what’s wrong, then fix the error.
Running this program results in an error. Read the error message and figure out what’s wrong, then fix the error.
Homework problems should always be your individual work. Please review the collaboration policy and ask the course staff if you have questions.
Double check your file names and return values. These need to be exact matches for you to get credit.
The email_splitter
function should split the email into a “name” and a “domain,” returning a tuple
name
is what comes before the @
symbol
domain
is what comes after the @
symbol
The change_email
function should call the email_splitter
function, and return the original email address, with the domain changed to gwu.edu
.
change_email.py
Calling change_email('george.washington@gmail.com')
should return string 'george.washington@gwu.edu'
It has several bugs. Fix them and submit as change_email.py
.
The check_lower
function should read in words from the file specified by filename
and return a list of booleans for whether each word is lowercase or not.
For instance, if the file contains the following:
Make sure you use regular expressions and account for punctuation.
The return value should be:
[False, True, True, True, True, True, True, True, True, True]
lower_check.py
The program has bugs. Fix them and submit as lower_check.py
.
The largest_digit
function is supposed to take as argument a positive integer, and return the largest digit. The return value should be an integer.
largest_digit.py
largest_digit(82)
should return 8
largest_digit(1012)
should return 2
The program contains several bugs. Fix them, and submit as largest_digit.py
.