0

Like I stated in the title I'm building a program that just multiplies but I want to ask the user if they want to continue or not. well, I have everything working but the "else" the if statement just passes over it.

def restart():
    pass

def multiply():
    while True:
        try:
            a = int(input("enter something: ", ))
            b = int(input("enter something: ", ))
            sum = a * b
            print(sum)
            restart()
            continuing = input("Would you like to continue: ", )
            if(continuing == "yes" or "no"):
                if(continuing == "yes"):
                    multiply()
                elif(continuing == "no"):
                    break
            else:
                restart()
            break
        except ValueError:
            print("you have to enter a number sorry you have to start over")
            multiply()
            break

multiply()

I tried messing with the breaks but it doesn't fix it. any ideas?

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Sayse Mar 20 '21 at 19:24
  • it doesn't sadly –  Mar 20 '21 at 20:19

3 Answers3

0

You never moves to else statement, coz:

if(continuing == "yes" or "no"):

in your or statement, even if continuing == "yes" is False, "no" is always True, coz it's not empty string like "", you should use, continuing == "no", like:

if(continuing == "yes" or continuing == "no"):

and whole code:

def restart():
    pass

def multiply():
    while True:
        try:
            a = int(input("enter something: ", ))
            b = int(input("enter something: ", ))
            sum = a * b
            print(sum)
            restart()
            continuing = input("Would you like to continue: ", )
            if(continuing == "yes" or continuing == "no"):
                if(continuing == "yes"):
                    multiply()
                elif(continuing == "no"):
                    break
            else:
                restart()
            break
        except ValueError:
            print("you have to enter a number sorry you have to start over")
            multiply()
            break

multiply()
Vova
  • 3,117
  • 2
  • 15
  • 23
  • So i thought that would work but i doesn't still passes over it –  Mar 20 '21 at 20:18
  • @DemetriusAlize how did you get it doesn't move to else?have you tried to debug? – Vova Mar 20 '21 at 20:21
  • @DemetriusAlize just checked it out, all good for me. after no -> breaks. if I pass smth other than yes or no, it breaks as well. please check it out – Vova Mar 20 '21 at 20:23
  • why do you use restart() if it does nothing, should not be there multiply() after else statement? – Vova Mar 20 '21 at 20:31
0

To fix your current issue, you can just change

if(continuing == "yes" or "no"):

to

if(continuing == "yes" or continuing == "no"):

When you want to check more than one condition of a variable, you need to explicitly write that variable and the condition.

Also, if you want, you could reduce the number of if/else statements you had. Like, instead of:

if(continuing == "yes" or continuing == "no"):
    if(continuing == "yes"):
        multiply()
    elif(continuing == "no"):
        break
else:
    restart()
break

You could write all that as just:

if continuing == "yes":
    multiply()
break

Because, from the current code you've shown, the only case you call multiply() is when continuing is "yes". If continuing is "no" or any other string, it hits a break.

EDIT: I see you want to continue looping until the user prints "yes" or "no", then in that case the issue is in the placement of the while loop.

You need to move the while loop in so it repeats the continuing section. Also, avoid naming your variables sum as that's already a built-in function in Python. You can try something like this:

def multiply():
    try:
        a = int(input("enter something: ", ))
        b = int(input("enter something: ", ))
        product = a * b
        print(product)
        while True:
            continuing = input("Would you like to continue: ")
            if continuing == "yes":
                multiply()
            elif continuing == "no":
                break
            else:
                print("Invalid answer, type 'yes' or 'no'")
        except ValueError:
            print("you have to enter a number sorry you have to start over")
            multiply()
            break
zucculent
  • 16
  • 1
  • 1
  • So i thought that would work but i doesn't still passes over it –  Mar 20 '21 at 20:13
  • The thing is i want it to keep looping till they type yes or no –  Mar 20 '21 at 20:19
  • @DemetriusAlize Ah I see, I updated my answer then with a possible fix, let me know if it works for you. – zucculent Mar 20 '21 at 21:30
  • Thanks, that works better, I just used two while loops are it does work now thanks. sorry for the late reply –  Mar 21 '21 at 13:43
0

Beside:

if(continuing == "yes" or continuing == "no"):

you also can write:

if(continuing in ["yes","no"]):
areop-enap
  • 396
  • 1
  • 7