Jan-25-2020, 10:27 PM
I am new to python and I'm having trouble with my calculator looping. I am trying to get this output below. I'm having trouble with getting the loop to work properly. My code is below the output I am thankful for any help or tips you can provide me.
create a function IsinRange() to test a value between two ranges
Enter your Lower range ---> 10
Enter your Higher range ---> 20
Enter your First number ---> 15
Enter your Second number ---> 17
The Result of 15.0+17.0=32.0
The Result of 15.0-17.0=-2.0
The Result of 15.0*17.0=255.0
The Result of 15.0/17.0=0.882352941176
Continue Looping Y/N Y
Enter your Lower range ---> 20
Enter your Higher range ---> 30
Enter your First number ---> 25
Enter your Second number ---> 50
The input values are out side the input ranges
Please check the numbers and try again
Thanks for using our calculator
My Code:
create a function IsinRange() to test a value between two ranges
Enter your Lower range ---> 10
Enter your Higher range ---> 20
Enter your First number ---> 15
Enter your Second number ---> 17
The Result of 15.0+17.0=32.0
The Result of 15.0-17.0=-2.0
The Result of 15.0*17.0=255.0
The Result of 15.0/17.0=0.882352941176
Continue Looping Y/N Y
Enter your Lower range ---> 20
Enter your Higher range ---> 30
Enter your First number ---> 25
Enter your Second number ---> 50
The input values are out side the input ranges
Please check the numbers and try again
Thanks for using our calculator
My Code:
def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): try: return x / y except ZeroDivisionError: return "You cannot divide by zero" def IsInRange(number_1, number_2, loRange, hiRange): if number_1 in range(loRange,hiRange) and number_2 in range(loRange,hiRange): return True else: return False cont = "Y" while cont == "Y" or cont == "y": loRange = int(input('Enter your Lower range: ')) hiRange = int(input('Enter your Higher range: ')) while True: try: number_1=int(input("Enter your first number:")) except ValueError: print ("Enter a number in the range of -100 to 100") else: if -100 <= number_1 <= 100: break else: print ("Enter a number in the range of -100 to 100") while True: try: number_2=int(input("Enter your second number:")) except ValueError: print ("Enter a number in the range of -100 to 100") else: if -100 <= number_2 <= 100: break else: print ("Enter a number in the range of -100 to 100") cont = input("Continue Looping Y/N:") print('The Result of', number_1, '+' ,number_2, '=', add(number_1,number_2)) print('The Result of', number_1, '-' ,number_2, '=', subtract(number_1,number_2)) print('The Result of', number_1, '*' ,number_2, '=', multiply(number_1,number_2)) print('The Result of', number_1, '/' ,number_2, '=', divide(number_1,number_2)) print('Thanks for using our calculator!')