How do I have multiple optional inputs?

Viewed 43
import time

x = input("Buy Eggs? (Y/N) ")

if x.lower().strip() == "yes" or "y":
    print("Buy Eggs")
    time.sleep(0.5)
    print("You bought eggs")

elif x.lower().strip() == "no" or "n":
    print("Don't buy eggs")
    time.sleep(.5)
    print("You don't have eggs")

What I want to happen is to allow multiple words to be inputted for one if command. I thought it'd work but instead of working it would prioritize the top piece of code.

Buy Eggs? (Y/N) n #<--- Answer#
Buy Eggs
You bought eggs

Process finished with exit code 0

It only seems to work if I remove the or command but I haven't found any other options to allow multiple optional inputs.

Thank you if you do choose to help me

2 Answers

Hope this can help you:

ans = input("Buy Eggs? (Y/N) ")

if ans.lower().strip() in ('y', 'yes'):
    print("Buy Eggs")
    time.sleep(0.5)
    print("You bought eggs")

elif ans.lower().strip() in ('n', 'no'):
    print("Don't buy eggs")
    time.sleep(.5)
    print("You don't have eggs")

I think you can give the multiple inputs in a list :

import time

x = input("Buy Eggs? (Y/N) ")

if x in ["yes",'y', "Y", "yea"]:
    print("Buy Eggs")
    time.sleep(0.5)
    print("You bought eggs")

elif x in ["no", "n", "NO", "N"]:
    print("Don't buy eggs")
    time.sleep(.5)
    print("You don't have eggs")
Related