Debugging in python, fun and easy!
Debug Without Errors
Debugging is the most dreaded part of programming. It should just work! And sometimes bugs can lead to hours of extra work. However they don't need to be, and by following some of the steps presented in this guide, hopefully you'll be able to fight your next bugs with vigor and valiance!
In this tutorial, we'll be using this program, which is intentionally broken. Follow along and make the changes we talk about, and see it be fixed right before your eyes! (Don't try to figure out what it does, it just does random things).
Alright, so when we run this program we get...
Oh no! We didn't even get an exception! Where do we even begin? Forchanetly, this program is very small. However for larger projects this is a huge issues. A smart place to start, would be the line that displays the output. In this case, it's
Now, in this program we only want this error to be raised in the max_number is 0! Clearly, the condition that must be met to trigger this line is wrong. The condition in this scenario is a try-except case, with no defined exceptions! This is bad code 101, also define what exception you want to catch, else wise python will catch all of them. Plus, the message doesn't need to be in a try-except statement. We can create an if-statement at the start of the function, where is the max number is 0, it will print the message and exit the function. If we remove the try-except statement and add the if statement, the function will look like this:
Great problem solved and if we run that...
What? We just fixed this error! Why is it still here? Don't panic, we know it's not the try-catch anymore. Think about it reasonably, this means that the code calling the function is calling it wrong, so let's take a look at it.
This is a great time to use the rubber duck technique. The idea is you read out your code to a rubber duck, although the rubber duck is optional. So let's do just that, we make a list, create a range from 0-3, call
handle_math with 5 and the item in the list at an index from 0-3. Then we update the items in the list by adding the result of handle_math to the start of the list. Did you catch the error? Maybe we should visualize what's happening. The list when we start looks like the this with
i being 0.
handle_math we return None, because the value passed in was 0. Then None is added to the start of the list, and then the loop increments
i so it's now
1, however now the list looks like:
As you can see, the 0 moved up, and so it's index is
1. The range generator doesn't compensate for that, and so
i will always be
0. To fix this, instead of iterating the index, let's iterate through each element in the list, which will look like:
Phew, we're finally done! Now if we run this we'll get:
Oh no, another bug! When will it end. Don't fret, we're actually making progress, and we can locate the current error with more ease! The problem now is this error message is super complicated.
'ascii' codec can't decode byte 0x81 in position 4: ordinal not in range(128)? What does that even mean? Errors like these are can be intimidating, but through the power of google, we will prevail! If you simply paste your exception into google, the first site is a stackover flow post which has a response that goes into great detail. If we read that, we'll realize that the issue is we're decoding bytes into ascii, which can't represent all the values in our data. Instead, we should use uft-8, which can handle all these values. Now that function looks like:
Wew, nice! Now that we've done our research, we should be in the clear right? Running our code now gives us:
Which is actually what we wanted! We're done! I know, this looks random and trust me it is, I just needed a broken program. I hope you learned much about debugging, and maybe I'll make a follow up using
pdb if this is well received. You can find the finished, debugged version here.
Have a great day!
Hello , please can you tell me how to run the repl debugger for a python 3 program where you can see variable contents etc... a standard debugger??? the repl help is very confusing.. I cannot see a debugger button or option in reply python 3