Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator Loop Help
#1
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!')
Reply
#2
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?
Reply
#3
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
Reply
#4
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python calculator help but not using while loop with true, any flags variable ,break kirt6405 13 5,726 Jun-08-2021, 06:39 AM
Last Post: Larz60+
  Weight loss calculator loop error drogers10940 7 14,518 Feb-14-2021, 12:36 AM
Last Post: Larz60+
  while loop on a calculator missus_brown 3 15,074 Feb-10-2019, 08:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020