To start this assignment, download this zip file.
Lab 3g — Debugging
Preparation
1 minute
Download the zip file for this lab, located above. This zip file has code that
you will use for this assignment. Extract the files and put them in your cs110
directory in a folder called lab3g
.
Code Quality
15 minutes
You have been given three different files for the personal_library problem you solved early this unit.
Open personal_library_worse_bug.py
and take a few minutes to see if you can find the five bugs hidden within.
After looking at that file, open personal_library_bug.py
. This file contains the same bugs as the last file.
If you didn’t find the bugs from the last file, see if you can find them here.
Compare how much easier it is to find them when the code is more decomposed.
Now open personal_library_type_hints.py
. Again this file has the same five bugs.
Notice that when we add type-hints, PyCharm points out some of the bugs to you.
Compare how much easier it is to find bugs when we are using type-hints.
Discussion
Which of the three files was the easiest to find the bugs in? Why?
How does decomposition change the difficulty of finding bugs?
What things could you change, to make finding bugs easier in your code?
Syntax Errors
5 minutes
A syntax error is an error that occurs when the code you are running doesn’t have the correct format. Most of the time PyCharm will tell you this with a red squiggly line under the part of the code that will give you a syntax error.
Open pokemon_syntax_bug.py
. This file has three sytnax errors within.
See if you can fix them. The test files have been provided for you to make sure you have fixed all the bugs.
Runtime Errors
5 minutes
A runtime error is an error that occurs while the program is running (i.e. at “runtime”). These errors can take a litle more time to find as PyCharm can’t identify them just by looking at your code.
Open pokemon_runtime_bug.py
. This file produces three runtime errors.
See if you can fix them. The test files have been provided for you to make sure you have fixed all the bugs.
Hint: Reading the Stack Trace
When python encounters a critical error at runtime, it “raises” an exception which causes your program to crash. As your program exits, python prints a stack trace of the error.
Let’s go over how to read the stack trace when you get an error.
- Always start at the last line.
- This line tells you exactly what is wrong with the code. In this case it is saying that the function
append()
can only have one thing in the parentheses, but 3 things were put in the parentheses. - If you don’t understand what this line means, try looking it up online, or even with ChatGPT. These tools are good for understanding these errors.
- This line tells you exactly what is wrong with the code. In this case it is saying that the function
- Look at the line immediately above it:
- The arrows (^) will point to the place that is wrong, the line number will tell you which line of code that phrase is in (line 9), and then the function name of what function it is in (
get_pokemon
)
- The arrows (^) will point to the place that is wrong, the line number will tell you which line of code that phrase is in (line 9), and then the function name of what function it is in (
- Most everything else above won’t be super relevant for this class at least, but just tells you more about the source of the bug and how to get there.
Incorrect Output
20 minutes
Sometimes our code works just fine, but it doesn’t do what we want it to do. These bugs tend to be the hardest to fix because it relies on our knowledge of the code and our ability to use the debugger to find them.
Part 1
Open up the first file for complex debugging (custom_bullets_bug.py
). This file only contains one bug. Follow the following steps to find the bug:
- Understand what the bug is
- Either run the program yourself or run the test and click to see the difference
- Use the Debugger
- If you still don’t have any idea where the bug might have occurred, place a breakpoint at the beginning of the main() function and start debugging.
- If you are a little more confident about where the bug might be, you can choose to place the breakpoint at a different spot
- Fix the bug
- Replace the wrong code with what you expect to be the right code
- Check to make sure the bug was fixed
- If you are very confident that you fixed the bug, run the test
- If you aren’t as confident, use the debugger to step through the code and see if you are getting the result you desire
Part 2
Open up the second file (date_ideas_bug.py
). This file contains more than one bug that has to be fixed. Using the steps above, try to find each of the bugs contained within. When you are passing the tests, you have completed this assignement!
We are providing a solution so you can check your work. Please look at this after you complete the assignment. 😊