Docsity

Python Program for Safe Division of Numbers, Lecture notes of Accounting

Toggle sidebar
bg1
BIO 340 For this problem, you will create a program that calculates the
division of two numbers entered by a user. The challenge is that the user can
enter anything, even non-numbers like strings. You need to ensure that your
program can handle valid and invalid input correctly and provide appropriate
feedback to the user without the program crashing. Follow these steps:
Prompt the user to enter two numbers, one at a time. You should
prompt the user for the first number, get the input, then prompt the
user for the second number, and get the input. Store these numbers in
variables num1 and num2.
Write a function named safe_division that takes in two arguments
num1 and num2. This function should perform the division of num1
by num2 and return the result. This function should be able to handle
any ValueError or ZeroDivisionError that might occur during the
execution and return an appropriate error message.
Call your safe_division function with num1 and num2 as arguments.
Print the result.
Ensure your program does not terminate unexpectedly regardless of
the input.
Provide comments in your code to explain the logic and error-
handling techniques used.
pf3
pf4

Partial preview of the text

Download Python Program for Safe Division of Numbers and more Lecture notes Accounting in PDF only on Docsity!

BIO 340 For this problem, you will create a program that calculates the division of two numbers entered by a user. The challenge is that the user can enter anything, even non-numbers like strings. You need to ensure that your program can handle valid and invalid input correctly and provide appropriate feedback to the user without the program crashing. Follow these steps: Prompt the user to enter two numbers, one at a time. You should prompt the user for the first number, get the input, then prompt the user for the second number, and get the input. Store these numbers in variables num1 and num2. Write a function named safe_division that takes in two arguments num1 and num2. This function should perform the division of num by num2 and return the result. This function should be able to handle any ValueError or ZeroDivisionError that might occur during the execution and return an appropriate error message. Call your safe_division function with num1 and num2 as arguments. Print the result. Ensure your program does not terminate unexpectedly regardless of the input. Provide comments in your code to explain the logic and error- handling techniques used.

Test your program with various inputs including valid and invalid numerical inputs, zero, and non-numeric inputs. It must be able to handle valid integers and floating-point numbers. Wrap all of this into a while loop to let the user continue trying until they decide to stop. Sure, I can help you with that. Here is a Python program that fulfills your requirements: def safe_division(num1, num2): """ This function takes two arguments and performs division. It handles ValueError and ZeroDivisionError exceptions. """ try: return num1 / num except ZeroDivisionError: return "Error: Division by zero is not allowed." except ValueError: return "Error: Invalid input. Please enter a number."

The safe_division function uses a try/except block to handle ZeroDivisionError and ValueError exceptions. If a ZeroDivisionError occurs, the function returns a message indicating that division by zero is not allowed. If a ValueError occurs, the function returns a message indicating that the input was invalid. The main part of the program is wrapped in a while loop, which continues to run until the user decides to stop. If the user enters a non-numeric input when prompted for the numbers, the program handles the ValueError exception and prints an error message. The program then asks the user if they want to continue. If the user enters anything other than "yes", the program stops.