An initial example of Input Error Handling with ZeroDivisionError in Python

https://miro.medium.com/max/1200/0*S2u5_mOXiauB-m9l

Original Source Here

An initial example of Input Error Handling with ZeroDivisionError in Python

After having learned some elementary Python code, I decided to study the programming language more indepthlessly. I have therefore recently studied error handling, which is code that prevents and / or corrects faulty input. The link to my previous post on error handling in Python is here:- An initial example of Input Error Handling in Python | by Tracyrenee | CodeX | Jun, 2021 | Medium

In this post I am going to discuss the ZeroDivisionError in Python, which arises whenever someone tries to divide a number by zero. In mathematics, when a number is divided by zero, the result is infinity. It is impossible for a computer to produce a number that is infinity, so a “division by zero” error is produced whenever this occurs.

In order to illustrate how the “division by zero” error works in Python, I have written a very small script to prove my point. In this script I have created a scenario where a person is going to have a picnic and make fried chicken to eat at the picnic:-

The user is asked to input how many pieces of chicken he has made. He is then asked to enter the number of people who will be at the picnic. The total figure is calculated as being the number of pieces of chicken by the number of people attending the picnic.

If the user enters zero when he is asked to enter how many people will be attending the picnic, the total amount of pieces of chicken per person will be infinity. Since the computer cannot create a value of infinity, a “division by zero” error occurs.

The screenshot below shows the error statement that is produced whenever 0 is entered into the second input prompt:-

In the screenshot below, I have endeavoured to make an amendment to the script to incorporate error handling:-

The first part of the statement uses the try: statement. The user is asked to enter how many pieces of fried chicken he has made and how many people will be attending the picnic.

A variable, num_each, is created by using floor division to divide the number of pieces of chicken by the number of people attending the picnic and returning the quotient.

A variable, leftover, is created to place the remainder in the division of chicken by picnic.

If the variable picnic has a 0 in it, the variable num_each will be infinity, which will produce a “division by zero” error. I have endeavoured to compensate for this error by inserting the except ZeroDivisionError: statement. By using the except ZeroDivisionError: statement, the variable num_each becomes 0 and leftover becomes the value of chicken. This means that nobody will get any chicken and the leftovers are the total pieces of chicken that had been initially entered.

The script will then print out a statement using the in-built format() function, saying there will be 0 pieces of chicken for each person and the leftovers will be the number of chicken pieces entered at the beginning of the program:-

In conclusion, I have shown two scripts, one with no error handling and another with ZeroDivisionError handling. Whenever writing a program, it is important to include error handling mechanisms to ensure the program does not crash because of improper inputting of data.

The code for this small program can be found in my personal GitHub account, the link being here:- Udacity-Course/Udacity_Intro_to_Python.ipynb at main · TracyRenee61/Udacity-Course (github.com)

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: