WEBVTT

00:00.000 --> 00:04.000
Debugging and troubleshooting Python code.

00:04.000 --> 00:11.000
In this video, we will learn to identify common runtime errors like type error and zero division error.

00:11.000 --> 00:16.000
Apply data type conversion, string to integer, correctly.

00:16.000 --> 00:19.000
Implement error handling using conditional checks.

00:19.000 --> 00:24.000
Reflect on debugging steps and improve problem solving skills.

00:24.000 --> 00:26.000
Understanding the code.

00:26.000 --> 00:29.000
We started by reviewing the function calculate average.

00:29.000 --> 00:37.000
Its purpose is to take numbers as input from the user, one by one, and calculate the average after the user types done.

00:37.000 --> 00:43.000
The loop collects numbers continuously, adds them up, and finally prints the average.

00:43.000 --> 00:50.000
On the surface, the logic looks fine, but a closer look reveals some hidden bugs.

00:50.000 --> 00:53.000
Identifying the first error.

00:53.000 --> 00:58.000
When we first ran the code, we encountered a type error.

00:58.000 --> 01:06.000
This happened because the input from the user is a string, and the code tries to add it directly to the total, which is an integer.

01:06.000 --> 01:12.000
So, we converted the user input to an integer before adding it to the total.

01:12.000 --> 01:15.000
Discovering the second error.

01:15.000 --> 01:19.000
After fixing the first issue, we entered numbers and then typed done.

01:19.000 --> 01:23.000
But the program still crashed with a zero division error.

01:23.000 --> 01:30.000
The code tried to divide total by count, but count was still zero, meaning no numbers were successfully counted.

01:30.000 --> 01:39.000
To fix this, we added count, plus equals to one after converting the input to an integer, so each valid number is counted.

01:39.000 --> 01:42.000
Then, we added a condition.

01:42.000 --> 01:47.000
If count is greater than zero, average equal to total divided by count.

01:47.000 --> 01:52.000
This ensures the division only happens when at least one number was entered.

01:52.000 --> 01:56.000
Otherwise, it shows. No numbers were entered.

01:56.000 --> 02:02.000
These errors were intentional and meant to test our debugging skills.

02:02.000 --> 02:04.000
Check the program.

02:04.000 --> 02:08.000
After applying the fixes, we ran the program again to test it.

02:08.000 --> 02:13.000
Entered numbers like 10, 20, 30, then typed done.

02:13.000 --> 02:16.000
The program correctly calculated and displayed.

02:16.000 --> 02:21.000
The average is 20.0.

02:21.000 --> 02:25.000
typed done immediately without entering any numbers.

02:25.000 --> 02:31.000
The program responded with no numbers were entered.

02:31.000 --> 02:37.000
The corrected code runs smoothly, handles inputs properly, and avoids common errors.

02:37.000 --> 02:43.000
Debugging helped improve problem solving and taught us to think from the user's perspective.

