Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im trying to make it so that if the user inputs something other that "stephen curry" or Stephen Curry", the else statement runs. But right now if I put something different, if still says you are correct. (By the way in the code below, the print statements are actually indented it just doesn't show)

What I have tried:

Python
A1 = raw_input("Who is the only warrior to have 2 MVP's?:")
if A1 == "stephen curry" or "Stephen Curry":
    print "you are correct"
else:
    print "sorry incorrect"
Posted
Updated 6-May-17 16:40pm
v3

if A1 == "stephen curry" or "Stephen Curry":

will always be true because it is not doing what you think it is doing. Due to the operator precedence rules your expression is being interpreted like this:
if (A1 == "stephen curry") or ("Stephen Curry"):

According to the Python docs a non-blank string evaluates to True when used in a condition so what this becomes is:
if (A1 == "stephen curry") or true:

which always resolves to true because anything or true = true. The way to fix this should be obvious but there are two other things to consider. First, look in to how you might use the in operator. Second, if you also want it to work for all case variations (e.g. "Stephen cURRy", "STEPHEN curry", etc.) look in to using the string "upper" method.
   
Quote:
How do I make the else statement work in the code?

The problem is not in the 'else', it is in the 'if' condition.
Try
Python
if A1 == "stephen curry" or A1 == "Stephen Curry":
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900