Debugging Principles and Techniques
Lesson Objectives
After this lesson, you will be able to…
Troubleshoot common types of errors.
Implement basic exception mitigation.
Troubleshoot logic errors.
Talking Points :
Reassure them that this won’t be awful: it is not really about their problem solving skills so much at this point as it is about getting them to notice the error message and decode what it is saying.
Discussion: Error Messages
Have you found a shiny red error message before? What do you think has happened here?
Teaching Tips :
Start a discussion here. What’s going on here? What other errors have the students encountered?
Talking Points :
“How many of you have run your code in a repl.it only to have a bunch of red text pop-up instead of your expected output?”
“What do you think is going on here in this image? How could it be fixed?”
Making Errors Into Friends
On the surface, errors are frustrating! However, we’ll walk through some common ones. You’ll see:
Errors sometimes say exactly what’s wrong.
Some errors have very common causes.
Errors may say exactly how to fix the issue.
Python errors are very helpful and have clear messages.
With that in mind - what’s the problem with this code?
Teaching Tips :
We introduce ZeroDivisionError first to start discussion, as just about everyone knows that you can’t divide by 0. Ask if the students think Python’s error message did a good job of informing us of what the issue was.
Talking Points :
“Python errors sometimes tell you exactly what to change about your code. For example, we see ZeroDivisionError: divide by zero, which is a really clear way to describe the issue.”
“Compared to other programming languages, Python errors are really nice! Think of them as helpful hints to help you make the best code possible!”
We Do: IndexError
Let’s debug this code together.
Protip : Index errors typically happen when you attempt to access a list index that doesn’t exist.
Teaching Tips :
This is a demo, not an exercise!
Teach the students how to read the line number where the error occurred. Remind them that the error often happens on the line above the line that threw the error.
Make sure that students come up with the solution themselves, but ask leading questions. For example, “Obviously this code isn’t working, but by looking at it, can you tell what my intention was? Which element do you think I was trying to access?”
Talking Points : - “Remember that lists are indexed starting at zero!” (maybe show a reminder)
Repl.it Note : This replit has
You Do: Fix a NameError
Directions: Fix it!
Hints : - Run the code to get the error. - What kind of error is it? What is the error message?
Teaching Tips :
Give them just a minute or two; then go over it. This is a new error, but it’s an easy one.
Talking Points : - “Let’s go back to an error we encountered earlier today!” - We most commonly get a NameError if we use a variable: * Without defining it. * Before defining it
Repl.it Note : This replit has
KeyError
Accessing a key in a dictionary that doesn’t exist.
Commonly caused by: - A misspelling. - Mixing uppercase and lowercase.
The error message tells you exactly what key is missing!
Teaching tip:
The repl.it is for you to demo this, not for an exercise.
Ask students what they think most commonly causes KeyErrors. Good answers might include case sensitivity in the key names, a typo in the key name, or simply not remembering what keys are available.
Talking Points :
“KeyError happens when you try to use a key in a dictionary that doesn’t exist”
“The error message tells you exactly what key threw the error”
Repl.it Note: It’s long; you might want to open it in a new tab. The replit has:
AttributeError
More general than KeyError, but the same idea.
Accessing an attribute (e.g., function or property) that doesn’t exist
Teaching tip:
The repl.it is for you to demo this, not for an exercise.
Talking Points : - “AttributeError is more general than KeyError (which only applies to dict keys), but the same general idea.”
Repl.it Note : The replit has:
Discussion: SyntaxError
Let’s run the code together. What happens? How can we fix it?
Teaching Tips :
See if they can pick up the answer.
Python is different (and better) than other languages in this regard, but it’s important to emphasize that = and == are for different purposes!
Talking Points : - “In any other language, (take JavaScript for example), if you accidentally use a single equals when you mean to use a double equals, the variable would be reassigned while inside that if statement and your 13 year old would be having a beer! Luckily the designers of Python knew this and made the choice to throw an error!”
Repl.it Note : The replit is
Discussion: TypeError
TypeError and its message tell us:
What do we learn from this error message? Have you learned a way to fix this?
Fun Fact : Some languages, like JavaScript, let this code run (breaking something!).
Talking Points :
We’re trying to combine different types in a way that doesn’t make sense
The error was caused when using the + operator
The error was caused by two incompatible types: string and integer.
Teaching Tips :
They learned how to do type conversion in the last lesson, so you could point out that they can cast 10 as an int and then this will work.
IndentationError
May be caused by:
Notenoughindentation
Mismatched indentation
Mixing tabs and spaces!
Teaching Tips :
This isn’t an exercise - just code to help you demo.
This is so common for Python beginners, so it is important to get practice recognizing this!
Have students come up with some examples of what might cause an error like this. Answers might include mixing tabs and spaces or indenting too much/too little.
Talking Points : - “IndentationError comes up for any indentation errors, whether it’s too little or just mismatching in some way”
Repl.it Note: this replit has
ValueError
Most commonly caused by trying to convert a bad string into a number.
Teaching Tips :
This is pretty straightforward, but pull up a repl.it if you think you need to.
RuntimeError
The worst error to see!
When no other error type fits.
You need to rely on the error message content.
May be used for custom errors.
Example : RuntimeError is like if I said to you:
Please eat the piano
You can understand what’s being asked, but can’t actually do that!
Talking Points :
“Unfortunately this is an error that comes up when no other error type fits the situation. You will need to rely heavily on the content of the error message rather than getting much of a hint from the type alone”
Quick Review
There are many types of errors in Python!
Usually, the error has a name or description that says exactly what’s wrong.
Think about IndentationError or IndexError - what went wrong?
Sometimes, you’ll see RuntimeError. Python throws us this if something is broken but it can’t say specifically what - like Please eat the piano. Revisit your code and see what might have happened.
Next Up: A list of common errors, then ways to prevent errors.
Teaching Tips :
This is a quick check for understanding.
See if they can tell you what those two errors mean.
List of Common Errors
This chart’s for you to refer to later - don’t memorize it now!
AttributeError
Attempting to access a non-existent attribute
KeyError
Attempting to access a non-existent key in a dict
ImportError
A module you tried to import doesn’t exist
IndexError
You attempted to access a list element that doesn’t exist
IndentationError
Indenting code in an invalid way
IOError
Accessing a file that doesn’t exist
NameError
Attempting to use a module you haven’t imported/installed
OverflowError
You made a number larger than the maximum size
RuntimeError
The error doesn’t fit into any other category
SyntaxError
A typo, such as forgetting a colon
TypeError
Using two different types in an incompatible way
ValueError
When you are trying to convert bad keyboard input to a number
ZeroDivisionError
Dividing By Zero
Teaching Tip:
Note that students haven’t learned all these yet - like modules or accessing files. Don’t go down this list in detail - just stress they can refer back to it.
Pause and take questions before switching gears.
Talking Points :
“Here is an alphabetically ordered list of the most common errors you may encounter when writing Python that you can refer back to.”
Discussion: Throwing Errors
Sometimes, we might have code that we expect to throw an error.
What if the user types a string like “Moose”?
This causes a ValueError - we’ll be trying to make an int out of a string “Moose”.
We can anticipate and prepare for it!
Teaching Tips :
Start by making sure they understand what the code does. They’ve seen input a few times, but won’t officially learn it for three more lessons; they just learned type casting.
Talking Points :
“Sometimes you may expect certain code to throw an error, and you may want to handle that situation with a smooth error message as opposed to having your whole program blow up with red text.”
Try-Except
A Try-Except block is the way we can catch errors in Python. We can catch:
One error (except ValueError:)
Multiple errors (except (ValueError, KeyError):)
Any/every error (except:)
Always try to specify the error, if possible!
Teaching Tips :
Try / except is new; do this with them. Make this a teaching slide, not an exercise.
Demonstrate some example output - strings, ints, floats, etc.
Talking Points :
Call out that we specifically say “ValueError”, and err is just a random keyword. Change it to demo. Add other error catches; take out the ValueError specifically.
“A Try-Except block is the way we can catch errors in Python.”
“We can catch one error (as we see in the code), we can catch multiple errors, or we can just catch any/every error by leaving it blank.”
“You can catch every possible error by leaving the specified error blank, however, this is generally not a great practice because it says very little about how you were thinking.”
Replit Note: The repl.it has
Discussion: Switching Gears
Not every programming error is caught by an error message!
Can anyone say what is wrong with this code?
What might happen if you run it?
Do not try to run the below code .
Teaching Tips :
See if anyone can tell you the problem (it’s an infinite loop).
Emphasize that an itty-bitty typo - just one character in this case - caused an enormous problem!
Talking Points :
“Errors and error messages are really helpful when we’ve got bad syntax or are trying to access something that doesn’t exist. However, not every possible programming error is able to be caught by error messages like this.”
“For example, consider the code here”
“Can you tell what the original intention of the code was? What went wrong? Why did the code go into an infinite loop? How can we fix this code?”
“There are many errors like this where there is nothing syntactically wrong with the code, but an error in the logic, or a typo that changes the meaning of the code as we saw with the infinite loop.”
Discussion: Another Infinite Loop
It’s easy to accidentally make an infinite loop. What’s the problem here?
Teaching Tips :
See if they can figure this out and tell you.
Talking Points :
“Let’s go through another common cause of infinite looping.”
“Putting ‘or’ when you mean ‘and’ is a common cause of pain!”
Infinite Infinite Loops!
Most common infinite loops are a result of:
A while loop’s condition never becomes False.
Forgetting to increment a counter variable.
Logic inside the loop that restarts the loop.
Bad logic in a while loop’s condition (e.g., putting or instead of and)
Be careful to check your end conditions!
If you find your program running endlessly, hit control-c in the terminal window to stop it!
Discussion: Logic Error
Here, we want to find the average of 8 and 10. The answer should be 9, because 8 + 10 == 18, then 18 / 2 == 9
What happened and why?
Teaching Tips :
Again, this is a teaching slide, not an exercise. Can they find the error?
When they do, write PEMDAS on the board and discuss the order of operations:
Parentheses
Exponents
Multiplication
Division
Addition
Subtraction
Replit Note: The code is:
Quick Review: Common Errors
If you expect an error, use a try/except block:
Logic problems are common but won’t throw a helpful error. Always check end conditions on your while loops!
Teaching Tips :
Do a quick check for understanding.
Print Statements for Sanity Checks
Pro Tip : If something is wonky and you don’t know why, starting printing.
Use print statements on each line to peek at the values.
Remember to remove debugging statements once the problem is solved!
When your programs become very complex, adding print statements will be a great help.
Teaching Tips :
Walk through the code and the print statements with the class.
Ask them to diagnose what happened
Talking Points :
“Something went wrong in that last bit of code! You may have already figured out what it is in this example, but in a more complex example, you might not know!”
After: “Why do you think splitting up the statement solved the problem?”
You Do: Wrapping it Up
Can you fix the code below?
5 minutes.
Teaching Tips :
It’s quite long; they probably want to open the repl.it in a new window. Remind them how to do that.
After 5 minutes or so (pending time), go over it with them.
Repl.it Note:: The code is:
new_phone = Phone(5214 )
class Phone:
def __init__ (self , phone_number):
self .number = phone_number
def call(self , other_number):
print ("Calling from" self .number, "to" , other_number)
def text(self , other_number, msg):
print ("Sending text from" , self .number, "to" , other_number
print (msg)
test_phone = Phone()
test_phone.call(515 )
test_phone.text(int ("text 141" ), "Hi!" )"
Summary and Q&A
Python has many common built-in errors.
Use try-except syntax to catch an expected error.
Logic issues don’t throw errors, so be careful!
Use print statements to walk through your code line-by-line.
Teaching Tips :
Check for understanding and wrap up.