This is my code. I've imported a library as "lib". when for some reason I receive a ZeroDivisionError when I enter a zero for my second number. I believe it's the dictionary causing the issue or maybe the function that the division element the dictionary points to. I'm not allowed to have any exception handling in the library. any pointers would be very much appreciated.
import Mylib as lib
def user_data():
try:
lower_range = int(input("Please enter a lower range value "))
higher_range = int(input("Please enter a higher range value "))
first_number = int(input("Please enter your first number "))
second_number = int(input("Please enter your second number "))
dictionary = {'add': lib.addition(first_number, second_number),
'sub': lib.subtraction(first_number, second_number),
'mult': lib.multiply(first_number, second_number),
'div': lib.division(first_number, second_number)}
if lib.is_in_range(lower_range, higher_range, first_number) and lib.is_in_range(lower_range, higher_range,
second_number):
menu_selection(first_number, second_number, dictionary)
else:
print("The input values are out side the input ranges")
print("Please check the numbers and try again")
print("Thanks for using our calculator")
run_again()
except ValueError:
print("You must enter a number. Please try again")
user_data()
def menu_selection(first_number, second_number, dictionary):
options = ['1) Add two numbers', '2) Subtract two numbers', '3) Multiply two numbers', '4) Divide two numbers',
'5) Scalc', '6) All in one']
print(*options, sep="\n")
user_option = input("Please select your option ")
if user_option == '1':
print(first_number, ' ', second_number, "=", dictionary['add'])
elif user_option == '2':
print(first_number, '-', second_number, "=", dictionary['sub'])
elif user_option == '3':
print(first_number, '*', second_number, "=", dictionary['mult'])
elif user_option == '4':
try:
print(first_number, '/', second_number, "=", dictionary['div'])
except ZeroDivisionError:
print("Can't divide a number by 0")
elif user_option == '5':
week_5()
elif user_option == '6':
print('All four calculations are', lib.allinone(first_number, second_number))
run_again()
def run_again():
finished = input("Would you like to run another calculation? Y or N ? ")
if finished == "Y":
user_data()
elif finished == "N":
input("Thank you for using the calculator. Press ENTER to exit")
exit()
else:
input("Invalid answer. Press Enter to exit")
exit()
def week_5():
try:
calc_string = input("Enter values in this format: number 1, number 2, operator to include the commas ")
print(lib.scalc(calc_string))
except ZeroDivisionError:
print("Can't divide a number by 0")
user_data()
Please see the lib library for further context
def scalc(p1):
lstring = p1.split(",")
if lstring[2] == "*":
res = multiply(int(lstring[0]), int(lstring[1]))
if lstring[2] == " ":
res = addition(int(lstring[0]), int(lstring[1]))
if lstring[2] == "-":
res = subtraction(int(lstring[0]), int(lstring[1]))
if lstring[2] == "/":
res = division(int(lstring[0]), int(lstring[1]))
return res
def addition(first_number, second_number):
return first_number second_number
def subtraction(first_number, second_number):
return first_number-second_number
def multiply(first_number, second_number):
return first_number*second_number
def division(first_number, second_number):
return first_number / second_number
def is_in_range(lower_range, higher_range, numbers):
return lower_range <= numbers <= higher_range
def allinone(first_number, second_number):
addition(first_number, second_number), \
subtraction(first_number, second_number),\
multiply(first_number, second_number), \
division(first_number, second_number)
CodePudding user response:
Since you cannot have error handling within the library, add a check for the second number before doing the calculation, see code below:
Since you cannot have error handling within the library, add a check for the second number before doing the calculation, see code below:
def menu_selection(first_number, second_number, dictionary):
options = ['1) Add two numbers', '2) Subtract two numbers', '3) Multiply two numbers', '4) Divide two numbers',
'5) Scalc', '6) All in one']
print(*options, sep="\n")
user_option = input("Please select your option ")
if user_option == '1':
print(first_number, ' ', second_number, "=", dictionary['add'])
elif user_option == '2':
print(first_number, '-', second_number, "=", dictionary['sub'])
elif user_option == '3':
print(first_number, '*', second_number, "=", dictionary['mult'])
elif user_option == '4':
if (second_number != 0):
print(first_number, '/', second_number, "=", dictionary['div'])
else:
print("Can't divide a number by 0")
elif user_option == '5':
week_5()
elif user_option == '6':
print('All four calculations are', lib.allinone(first_number, second_number))
run_again()
CodePudding user response:
Looks like the definition is listed in the error:
1 Traceback (most recent call last):
2 File "c:\Users\dduke\Documents\vsCode\stacks\zerodiv\test.py", line 75, in <module>
3 user_data()
4 File "c:\Users\dduke\Documents\vsCode\stacks\zerodiv\test.py", line 14, in user_data
5 'div': lib.division(first_number, second_number)}
6 File "c:\Users\dduke\Documents\vsCode\stacks\zerodiv\Mylib.py", line 27, in division
7 return first_number / second_number
8 ZeroDivisionError: division by zero
Notice specifically line 5 in the error. You can follow the progression of code to see how it ended up there.
After ended up on line 14 of test.py
, we're taken to 27 on Mylib.py
- which is trying to divide by zero.
Your function saw nothing wrong with the values provided and attempted to run the division code in test.py
.