python
def addition(n1, n2):
return n1 + n2
def subtraction(n1, n2):
return n1 - n2
def multiplication(n1, n2):
return n1 * n2
def division(n1, n2):
return n1 / n2
def IsInRange(lr, hr, n):
return lr <= n <= hr
class Error(Exception):
"""Base class for other exceptions"""
pass
class DivisionByZero(Error):
"""Raised when the some expression is being divided by zero"""
pass
class OutOfRange(Error):
"""Raised when the entered numbers are out of range"""
pass
def main():
low = int(input("Enter your Lower range:"))
high = int(input("Enter your Higher range:"))
num1 = int(input("Enter your First number:"))
num2 = int(input("Enter your Second number:"))
try:
if IsInRange(low, high, num1) and IsInRange(low, high, num2):
add = addition(num1, num2)
sub = subtraction(num1, num2)
mul = multiplication(num1, num2)
print("The result of" + str(num1) + "+" + str(num2) + "=" + str(add))
print("The result of" + str(num1) + "-" + str(num2) + "=" + str(sub))
print("The result of" + str(num1) + "*" + str(num2) + "=" + str(mul))
if num2 != 0:
div = division(num1, num2)
print("The result of %d.0/%d.0=%d.0" % (num1, num2, div))
else:
raise DivisionByZero
else:
raise OutOfRange
except OutOfRange:
print("""
The input values are outside the input ranges.
Please check numbers and try again.
Thanks for using our calculator!
""")
except DivisionByZero:
print("The result of %d.0/%d.0= DIVISION BY ZERO" % (num1, num2))
while True:
main()
user = input("Continue Looping Y/N:")
if user != "Y":
break
print("Thanks for using our calculator!")