-1

For some reason, no matter what user_input is (Yes, No, "", eserw3) the first if statement will always be triggered. Any insight as to why the elif and the else never get activated? (The below code compiles perfectly without any errors)

Thank you in advance.

def retry():
        user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy))
        if user_input == "Yes" or "yes":
            respawn()
            getMove()
        elif user_input == "No" or "no":
            print "Thanks for playing!"
        else:
            print "Please enter either Yes or No."

3 Answers3

0
def retry():
    user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy)).lower()
    if user_input == "yes":
        respawn()
        getMove()
    elif user_input == "no":
        print "Thanks for playing!"
    else:
        print "Please enter either Yes or No."
  • 2
    Even better: lowercase the input immediately. `user_input = raw_input(...).lower()`. Then the lower-casing does not need to be repeated and possibly forgotten on one branch. Feel free to edit your code with this improvement. – Terry Jan Reedy Mar 27 '16 at 07:30
0
def retry():
        user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy))
        if user_input == "Yes" or user_input == "yes":
            respawn()
            getMove()
        elif user_input == "No" or user_input == "no":
            print "Thanks for playing!"
        else:
            print "Please enter either Yes or No."
Guclu
  • 142
  • 11
-1

Change your if condition to

user_input in ["Yes", "yes"]

Reason: When you write user_input == "Yes" or "yes", it evaluates as:

(user_input == "Yes") or "yes"

The second part of OR is a True always(non-zero length string). Hence your problem of if block executing always.

Prajwal K R
  • 682
  • 5
  • 14