Question: why is my zero exception not working? #input . Enter choice(1/2/3/4/5/6 or q):4 lower range variable-100 higher range variable100 first variable0 Enter your second variable40
why is my zero exception not working?
#input
. Enter choice(1/2/3/4/5/6 or q):4
lower range variable-100
higher range variable100
first variable0
Enter your second variable40
Enter problem string like this, N1,N2,and Operator, 2,2,+
the quotient of: 0 / 40 = 0.0 Would like to try again? y for yes!
#code
from mylib import Operators
def IsInRange(low_range, high_range, number):
if number <= low_range:
return False
elif number >= high_range:
return False
else:
return True
def Menu():
print("Select Operation.")
print("1.Addition")
print("2.Subtraction")
print("3.Multiply")
print("4.Divide")
print("5.scalc")
print("6.All in one")
print(" ")
def main():
obj = Operators()
Menu()
choice = input("Enter choice(1/2/3/4/5/6 or q):")
try:
low_range = int(input("lower range variable"))
high_range= int(input("higher range variable"))
var1 = int(input("first variable"))
while not IsInRange(low_range, high_range, var1):
var1=int(input("Re-Enter first variable"))
var2=int(input("Enter your second variable"))
while not IsInRange(low_range, high_range, var2):
var2=int(input("Re-Enter your second variable in the range"))
except ValueError:
print('please use integers')
return
operation = str(input("Enter problem string like this, N1,N2,and Operator, "))
try:
if choice == '1':
print("the sum of", var1, "+", var2, "=", obj.addition(var1, var2))
elif choice == '2':
print("the difference of:", var1, "-", var2, "=", obj.subtraction(var1, var2))
elif choice == '3':
print("the product of:", var1, "*", var2, "=", obj.multiplication(var1, var2))
elif choice == '4':
print("the quotient of:", var1, "/", var2, "=", obj.division(var1, var2))
elif choice == '5':
print(obj.scalc(operation))
elif choice == '6':
print(obj.allInOne(var1,var2))
elif var1 >= low_range and var2 <= high_range:
print("the sum of", var1, "+", var2, "=", obj.addition(var1, var2))
print("the difference of", var1, "-", var2, "=", obj.subtraction(var1, var2))
print("the product of", var1, "*", var2, "=", obj.multiplication(var1, var2))
print("the quotient of", var1, "/", var2, "=", obj.division(var1, var2))
print(obj.scalc(operation))
print(obj.allInOne(var1,var2))
else:
print(IsInRange(low_range, high_range,int))
except ZeroDivisionError:
print("You cannot divide by zero!!")
restart = input("Would like to try again? y for yes!")
if restart == "y" :
main()
else:
print("Thank you for using the calculator.")
return
main()
#mylib
class Operators:
def addition(self,var1,var2):
return var1+var2
def subtraction(self,var1,var2):
return var1-var2
def division(self,var1, var2):
try:
return var1 / var2
except ZeroDivisionError as e:
print("Error: Cannot divide by zero!")
def multiplication(self,var1,var2):
return var1*var2
def scalc(self,p1):
istring = p1.split(",")
res = 0
if istring[2] == "+":
res = self.addition(int(istring[0]), int(istring[1]))
elif istring[2] == "-":
res = self.subtraction(int(istring[0]), int(istring[1]))
elif istring[2] == "*":
res = self.multiplication(int(istring[0]), int(istring[1]))
elif istring[2] == "/":
res = self.division(int(istring[0]), int(istring[1]))
return res
def allInOne(self,var1,var2):
result1= self.addition(var1, var2)
print("{0}+{1}={2}".format(var1,var2,result1))
result2= self.subtraction(var1, var2)
print("{0}-{1}={2}".format(var1,var2,result2))
result3= self.multiplication(var1, var2)
print("{0}*{1}={2}".format(var1,var2,result3))
result4= self.division(var1, var2)
print("{0}/{1}={2}".format(var1,var2,result4))
return {"addition":result1, "subtraction":result2,"multiplication":result3,"division":result4}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
