1

When I say "n" or "no" for the input if works fine but when i say "y" or "yes" for the input it just does the same thing as for "no". everything else in the program runs perfectly apart from this. i have absolutely no clue as to why this is happening.

def restart():
    replay = input("Do you want to restart? (Y/N): ")
    if replay.lower() == "n" or "no":
        print("exiting")
        sys.exit()
    if replay.lower() == "y" or "yes":
        calc()
    else:
        print("Unknown Command")
        restart()
restart()
Ahmad
  • 1,618
  • 5
  • 24
  • 46
Callum
  • 23
  • 2

2 Answers2

2

The conditions in your if statements aren't evaluating the way you think they are. When you use a logical operator like the or you have, it evaluates first the part before the or, and then the part after the or, and if either is true the whole statement is true.

So instead of

if replay.lower() == "n" or "no": #always runs because just "no" evaluates as being true

use

if replay.lower() == "n" or replay.lower == "no":

and make a similar change to your if statement which tests for yes.

Eric McKeeth
  • 347
  • 5
  • 11
0

Replace this:

    if replay.lower() == "n" or "no":
        print("exiting")
        sys.exit()
    if replay.lower() == "y" or "yes":
        calc()

With this:

    if replay.lower() == "n" or replay.lower() == "no":
        print("exiting")
        sys.exit()
    if replay.lower() == "y" or replay.lower() == "yes":
        calc()
AlphaZetta
  • 16
  • 4