我如何有多个可选输入?

  • 本文关键字: python
  • 更新时间 :
  • 英文 :

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")

我想要的是允许在一个if命令中输入多个单词。我认为它会工作,但它没有工作,而是优先考虑最上面的代码。

Buy Eggs? (Y/N) n #<--- Answer#
Buy Eggs
You bought eggs
Process finished with exit code 0

它似乎只工作,如果我删除or命令,但我还没有找到任何其他选项来允许多个可选输入。

如果你选择帮助我,谢谢你

希望对你有所帮助:

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")

我认为你可以在一个列表中给出多个输入:

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")

相关内容

  • 没有找到相关文章

最新更新