1
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

Bulrunord
  • 11
  • 2
  • 1
    Try like this `x.lower().strip() in ["yes", "y"]` – Liju Jul 09 '22 at 13:22
  • Actually, you'll see a bunch of different options, like `x in ["yes", "y"]` or `x in ("yes", "y")`, but you *should* use `x in {"yes", "y"}` instead, for performance reasons (assuming you care about performance). – jthulhu Jul 09 '22 at 13:34

2 Answers2

0

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")
Daniel Hao
  • 4,922
  • 3
  • 10
  • 23
0

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")
gsv
  • 74
  • 1
  • 12