1

I was wondering if somebody could tell me what is wrong with this code, when I run the code it shows nothing but if I take out the "elif" it does work.\

first=input("What is your first name? ");
middle=input("What is your middle name? ");
last=input("What is your last name? ");
test = [first, middle, last];
print ("");
print ("Firstname: " + test[0]);
print ("Middlename: " + test[1]);
print ("Lastname: " + test[2]);
print ("");
correct=input("This is the information you input, correct? ");
if (correct == "Yes" or "yes"):
    print ("Good!")
elif (correct == "no" or "No"):
    print ("Sorry about that there must be some error!");
2
  • 2
    People have pointed out that the tests in your if and else are probably not what you want, but are you sure it really prints nothing?? Commented Sep 24, 2013 at 17:00
  • To amplify @gnibbler, False or "yes"True and True or "yes"True therefore print("Good!") should always execute regardless of the value of correct. Commented Sep 25, 2013 at 1:51

3 Answers 3

6

Here's the problem:

if (correct == "Yes" or "yes"):
    # ...
elif (correct == "no" or "No"):
    # ...

It should be:

if correct in ("Yes", "yes"):
    # ...
elif correct in ("No", "no"):
    # ...

Notice that the right way to make a comparison involving several conditions is like this:

correct == "Yes" or correct == "yes"

But usually it gets written like this, which is shorter:

correct in ("Yes", "yes")
Sign up to request clarification or add additional context in comments.

1 Comment

I input that and it still doesn't have an output of anything.
3

You need to use the in keyword:

if correct in ("Yes", "yes"):
    print ("Good!")
elif correct in ("no", "No"):
    print ("Sorry about that there must be some error!")

or convert the entire input to the same case:

# I use the lower method of a string here to make the input all lowercase
correct=input("This is the information you input, correct? ").lower()
if correct == "yes":
    print ("Good!")
elif correct == "no":
    print ("Sorry about that there must be some error!")

Personally, I think the lower solution is the cleanest and best. Note however that it will make your script accept inputs such as "YeS", "yEs", etc. If this is a problem, go with the first solution.

Comments

1

You'e checking correct incorrectly

if (correct == "Yes" or "yes"):

means (correct == "Yes") or ("yes"), and non-empty string evaluates to True in python, so first condition will always be True.If you want to check multiple strings, you can do:

if (correct in ("Yes", "yes")):

But this one doesn't takes 'yEs' or 'yES' into account. If you want case-insensitive comparison, then I think correct.lower() == "yes" would be preferred method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.