Python Forum
Calculator Loop Help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Calculator Loop Help (/thread-23988.html)



Calculator Loop Help - dock1926 - Jan-25-2020

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:

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!')



RE: Calculator Loop Help - ibreeden - Jan-26-2020

You already have a lot. You only have to put it in the right order. The main loop would have to be "continue or not". Nearly everything should be inside that loop. Like this:
continue = "Y"
while continue == "Y":
    input1
    input2
    ...
    printresult1
    printresult2
    ...
    continue = input("Continue Looping Y/N:")

print('Thanks for using our calculator!')
Can you change your code according to that?


RE: Calculator Loop Help - dock1926 - Jan-26-2020

Thank you I finally got it. I was working on this for hours yesterday and was getting so frustrated I wanted to throw my laptop. I can't believe it was something simple like that. I also forgot to indent some lines but it's all good now and working. Thanks for the help


RE: Calculator Loop Help - HereweareSwole - Jun-23-2021

(Jan-26-2020, 06:29 PM)dock1926 Wrote: Thank you I finally got it. I was working on this for hours yesterday and was getting so frustrated I wanted to throw my laptop. I can't believe it was something simple like that. I also forgot to indent some lines but it's all good now and working. Thanks for the help

So what did it end up looking like?