0
i=0
while (i==0):

    n1=float(input("Enter the first number: \n"))
    n2=float(input("Enter the second number: \n"))
    a=str(input("Choose the operation: \n 1.) + \n 2.) - \n 3.) * \n 4.) / \n 5.) % \n"))
    if(a==1):
        print("The addition of the inputted numbers is ", sum(n1,n2),"\n")
    elif(a==2):
        print("The difference between the inputted numbers is ", diff(n1,n2),"\n")
    elif(a==3):
        print("The multiplication of the inputted numbers is ", product(n1,n2),"\n")
    elif(a==4):
        print("The division of the inputted numbers is ", div(n1,n2),"\n")
    elif(a==5):
        print("The modulus of the inputted numbers is ", modulus(n1,n2),"\n")
    else:
        print("Do you want to quit the calculator?")
        b=int(input("Press 00 for 'Yes' and 11 for 'No'\n"))
        if(b==00):
            break 
        else:
            print("Enter a valid option for the operation number.\nThe calculator is re-starting.")
            continue

This is my python code. I have defined all the functions used here.

The output I am getting is:

enter image description here

When I inputted the correct operation number 3 at the place I have circled in the image, why is the output not giving me the final calculated answer? Why is it repeating the same loop again and again?

Please help me in this.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Dynamo
  • 51
  • 8
  • 3
    Your inputs are strings, and you compare it to numbers. That can only fail. – Thierry Lathuille Jan 24 '21 at 20:34
  • 4
    Give that `continue` is the last possible statement that can execute in this loop, it's not necessary. Execution will continue to the top of the loop with or without it. – chepner Jan 24 '21 at 20:34
  • `00` is the same as `0` (a special case, as non-zero integer literals cannot begin with a `0`), which is what `int('00')` will return. – chepner Jan 24 '21 at 20:36
  • @ThierryLathuille The user may input anything and that is why we need to prompt the user in this program to enter any number operation from the ones given. – Dynamo Jan 24 '21 at 20:47
  • @chepner But how can I make the program to execute all of the instructions in the while loop again (including if-else) once it comes to the last else statement? – Dynamo Jan 24 '21 at 20:48
  • 1
    Unless you execute `break`, that's what happens automatically. `continue` is used to jump to the top of the loop even if there is still code below the `continue` statement that could be executed. – chepner Jan 24 '21 at 20:50
  • @chepner Please look at my output code. It is not executing the if-else statements (a==1), (a==2), and so on. – Dynamo Jan 24 '21 at 20:52
  • 1
    Your main issue is that `a` is a string, which will never be equal to any of the `int` values you're comparing it to. Fix `a = str(input(...))` to `a = int(input(...))` and your code will work. There is no issue with your `continue` statement. It's unnecessary, but harmless. – Blckknght Jan 24 '21 at 20:54
  • 1
    There's little reason to treat the input as an integer. `int("foo")` will raise an exception, so just compare `a` to the string `"1"` rather than the integer `1` (and likewise for the other cases). – chepner Jan 24 '21 at 21:02
  • Yes, got it. Thanks a lot. @chepner – Dynamo Jan 24 '21 at 21:04

2 Answers2

2

Hacker already provided you with all the changes that need to be done. You need to look at your integer vs string comparison, your while loop usage, and your Try-Except options.

To help you have a reference point, here's modified code for you to compare:

while True:

    while True:
        try:
            n1=float(input("Enter the first number: \n"))
            break
        except:
            print ('Enter a number. It can be integer or float')

    while True:
        try:
            n2=float(input("Enter the second number: \n"))
            break
        except:
            print ('Enter a number. It can be integer or float')

    while True:
        try:
            a=int(input("Choose the operation: \n 1.) + \n 2.) - \n 3.) * \n 4.) / \n 5.) % \n"))
            if 1 <= a <= 5:
                break
            else:
                print ('Enter a number within range 1 and 5')
        except:
            print ('Not a number. Please enter an integer')
    
    if   (a==1):
        print("The addition of the inputted numbers is ", (n1+n2),"\n")
    elif (a==2):
        print("The difference between the inputted numbers is ", (n1-n2),"\n")
    elif (a==3):
        print("The multiplication of the inputted numbers is ", (n1*n2),"\n")
    elif (a==4):
        print("The division of the inputted numbers is ", n1/n2,"\n")
    elif (a==5):
        print("The modulus of the inputted numbers is ", int(n1%n2),"\n")

    print("Do you want to quit the calculator?")

    while True:
        try:
            b=int(input("Press 00 for 'Yes' and 11 for 'No'\n"))
            if b==0: break
            elif b!= 11: print ('Enter a valid option: 00 or 11')
        except:
            print ('Not a number. Please enter an integer')
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • some of these can be converted to a function to get the values. I am keeping the similar structure so you can see how your code compared to what I wrote – Joe Ferndz Jan 24 '21 at 21:15
  • Thanks for acquainting me with a new approach to the problem! @Joe – Dynamo Jan 25 '21 at 18:48
  • 1
    For choose the operations, you really don't need to check the integer value. You can do a string compare of `'1'` instead of int(1). That will reduce your code and also remove the need for `try` and `except` – Joe Ferndz Jan 25 '21 at 18:53
1

I am just going to summarize the comments and add some stuff.

  1. The while loop will run forever if you do not break it because i is always going to be 0 because it is not changed inside of the loop.
  2. As Thierry Lathuille pointed ot friendly a is defined as a stra str can never be equal to a number: you would have to do:
    int(a)==1 or a==str(1)
  3. Your input was 11 and as expected the calculator said that he is going to restart and did not break.
  4. continue will restart the loop while break will stop it. As @chepner pointed out: if continue is the last possible statement then it doesn`t do anything.
  5. Also pointed out by @chepner is that 00 is equal to 0
Hacker
  • 180
  • 2
  • 14