-1

Ok i have an if then statement and i want it to display something if the user clicks Yes or No and if they enter something except YES OR NO then it gives an error. But when i do it, i put it in a print variable but even though they put something else it still goes on. Here is the code.

drink = raw_input("Do you want a drink?")
if (drink =="Yes" or drink =="yes"):
    print ("Soda\n Coffee \n Latte\n or Smoothie")
    drink1 = raw_input("What type of drink will you like?")
    print (drink1," coming up")
else:
    print ("Error")

1 Answer 1

6

Because your condition only has a condition for yes. So even if you take no as input, you will still go in to the error condition. You need an extra elif to handle the "no" case.

Also, just cast drink to lower and always check it for one case: "yes":

drink = raw_input("Do you want a drink?").lower()
if (drink == "yes"):
    print ("Soda\n Coffee \n Latte\n or Smoothie")
    drink1 = raw_input("What type of drink will you like?")
    print (drink1," coming up")
elif (drink == "no"):
    print("you do not want a drink")
    # do things for no here
else:
    print ("Error")
Sign up to request clarification or add additional context in comments.

8 Comments

what does the ".lower" do lol. Sorry we haven't learned that yet in class
.lower converts all the letters to lowercase in a specified string.
^^Yup. That would be it.
so its redundant to put like (blah =="yes" or blah=="Yes")
@SeanBallais You're absolutely right. Silly mistake. I corrected my solution. Thanks.
|

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.