Week 5 Python
# I have implemented an exception class named as out_of_range # I have implemented it to throw exception if the user enters any out of range number # The exception class is created at the top and the exception is raised under the while loop after input class out_of_range(Exception): pass def is_in_range(lr, hr, n): return n >= lr and n <= hr def add(n1, n2): return n1+n2 def sub(n1, n2): return n1-n2 def mult(n1, n2): return n1*n2 def div(n1, n2): return n1/n2 while 1: lr = float(input('Enter your Lower range ---> ')) hr = float(input('Enter your Higher range ---> ')) n1 = float(input('Enter your First number ---> ')) n2 = float(input('Enter your Second number ---> ')) if not (is_in_range(lr, hr, n1) and is_in_range(lr, hr, n2)): raise out_of_range print('The Result of ', n1, '+', n2, '=', add(n1, n2)) print('The Result of ', n1, '-', n2, '=', sub(n1, n2)) print('The Result of ', n1, '*', n2, '=', mult(n1, n2)) print('The Result of ', n1, '/', n2, '=', div(n1, n2)) rep = input('Continue Looping Y/N ') if rep == 'N' or rep == 'n': break print('Thanks for using our calculator')