user3085339
user3085339

Reputation: 13

If statement evaluating true no matter what. Python 3.3.0

title=("The Game")
print (title)
start = input("Begin?")
if start == "no" or "n":
    print ("Too Bad")
    import antigravity;
if start == "yes" or "y":
    print ("Welcome to the Experiment")
else:
    print ("IDK");

No matter what my response is, the first "if" will always be resolved as true.

Upvotes: 1

Views: 137

Answers (1)

joemaller
joemaller

Reputation: 20616

The if statement isn't doing what you think it is. Python is evaluating the first comparison, start == "no" then comparing that to to "n", which being a non-empty string, is always true. Essentially it was (start == "no") or "n".

This is what you probably meant:

if start == "no" or start == "n":

but that's not really the python way. This is what you're looking for:

if start in ["no", "n"]:

That checks to see if the string value of start is in the list of acceptable string values. You'll probably also want to compare lowercase values only with something like start.lower()

Upvotes: 2

Related Questions