In earlier article,you learned about different python errors. In this article, we will talk about how to debug those errors. There are several techniques, but we will discuss some practical ways to fix errors in python programs.
Types of Debugging in Python
Three types of debugging technique that you can employ in python programming are:
- Print debugging technique
- Scope debugging technique
- Squeeze Debugging technique
You can use any or all other methods mentioned above in your python programming. The ultimate goal should be to fix the error.
Print Debugging
The print debugging
is a technique that you can use to add extra lines of code usually a print statement to check the status of variables or block of code in python program. Consider the following example.
principal = 25000 rate = 0.12 time_in_year = 3 simple_interest = principal * rate * time_in_year total_amount = principle + simple_interest + simple_interest print(total_amount)
In the program above, there is no error. However, the program does a mistake of adding simple_interest
two times resulting in 43000
as total_amount
. That is incorrect.
To debug the incorrect result error, you need to check the value of simple_interest using a print statement
.See the modified code below.
principal = 25000 rate = 0.12 time_in_year = 3 simple_interest = principal * rate * time_in_year print(simple_interest) total_amount = principle + simple_interest print(total_amount) ## Output following 9000 34000
Now the, you can correct the code and output should be 34000 after removing the second simple_interest in the code.
Scope Debugging
The error or the problem might be in a block of code resulting in a wrong output. Add print debugging and scope debugging to debug your program block by block would be a good idea.
In the following example, we are trying to take average of a list of marks for a student. There is an error in for loop that results in incorrect output, therefore, we are using both print debugging and scope debugging.
myMarks = [23,55,66,52,99] # get the total marks of the student sum = 0 # get the number of subjects count = 0 for mark in myMarks: sum = mark count = count + 1 print(sum/count)
The program prints following output which does not seems right. The value of average is too low.
================ RESTART: C:/Python_projects/Average_Marks.py ================ 19.8
We need to add a print statement above the for loop
and inside the block to check the difference. This is scope debugging
because we are checking the block separately. See the modified version of the program below.
myMarks = [23,55,66,52,99] # get the total marks of the student sum = 0 # get the number of subjects count = 0 print(sum) for mark in myMarks: sum = mark print(sum) count = count + 1 print(sum/count)
After running the program again, we get following outputs.
================ RESTART: C:/Python_projects/Average_Marks.py ================ 0 23 55 66 52 99 19.8 >>>
The sum variable starts with 0 and then enters the loop. It is not incrementing each time a new iteration starts. The final average is 19.8 which is incorrect. To fix the problem we have to add the previous sum
to the new sum
at each iteration.
myMarks = [23,55,66,52,99] # get the total marks of the student sum = 0 # get the number of subjects count = 0 for mark in myMarks: sum = sum + mark count = count + 1 print(sum/count)
Now, we will get the correct output as follows.
================ RESTART: C:/Python_projects/Average_Marks.py ================ 59.0 >>>
Note: make sure that you remove all the debugging print statements before saving your program for the final time.
Squeeze Debugging
Sometimes that programs are large and complex and using print debugging or scope debugging is not enough. The squeeze debugging technique uses a method of solving the logical programming problem by explaining the logic in detail.
When explain the problem and each step of the program in detail, it gives you opportunity to check your program in-depth and debug it completely. Later, you can combine other debugging methods to solve an error in the program.