-2

So I'm doing a calculator in python, I have the code for it inside a while loop and a prompt to ask the user if they want to restart the program (that's what the while loop is for) but it does not seem to work properly, am I missing something? I'm kinda new to programming so maybe there is something blatantly wrong that I just don't see. (I did not add the full code but I did import the necessary libraries ("sys" and "math"))

var1 = True

while var1 == True:

    op = eval(input("\n Your operation: "))

    print(f"\n The result would be: {op}")

    var2 = input("\n Would you like to do another operation? If so type yes: ")

    if var2 != "yes" or "Yes" or "YES" or "y" or "Y" or "1":
        print("\n Ok then, exiting... \n")
        sys.exit()

So if the user types, for example, "yes" in the prompt, it should restart the program, but it executes the if statement anyways and closes the program even though the condition for it doesn't apply.

I have tried adding an "else" statement like this:

if var2 != ... :
   sys.exit()
else:
   print("Restarting...")

But it doesn't seem to work either.

I've also tried doing it the other way around, that is instead of checking if it does not match, check if it does match. Like this:

if var2 == ... :
   print("Restarting...")
else:
   sys.exit()

But that just gets stuck in the while loop and does not close the program.

I just don't see what's wrong in the code.

The correct way of doing it would be:

if var2.lower() not in ("yes", "1"):
   print("Ok then, exiting...")
   sys.exit()
Niquel
  • 1
  • 3

1 Answers1

0

You’re logic is backwards you need to exit if it ISNT yes also instead of using or (plus you’re using it incorrectly) use in and instead of typing all the different variations of Yes use star.lower():

var1 = True

while var1 == True:

    op = eval(input("\n Your operation: "))

    print(f"\n The result would be: {op}")

    var2 = input("\n Would you like to do another operation? If so type yes: ")

    if var2.lower() not in ("yes", "1"):
        print("\n Ok then, exiting... \n")
        sys.exit()
Jab
  • 26,853
  • 21
  • 75
  • 114