python - 如何使用 if 语句来响应几个不同的字符串选项
问题描述
我正在用 python 制作比萨店体验,但遇到了一些麻烦,因为我想知道如何在 if 语句中添加几个字符串选项。
这是我要运行的代码:
menu = (" Happy Ham's Pizza Hut \n\nSIZES \nLarge pizza (20in.) \nMedium pizza (15in.) \nSmall pizza (personel 10in.)\n")
menu += ("\nTYPES \nVegan pizza(non-dairy cheese, tofu pepporoni)\nVegatarian pizza(ground-corn crust, graded radish cheese alternative, cucumber rounds)")
name = str(input(" \n Hello, and welcome to happy ham's pizza hut! \nWould you like a menu? \n >>"))
if name == ('yes, yes please'):
print(menu)
现在的问题是在制作 if 语句时,我希望它对几个不同的答案有相同的响应。我怎么做?
解决方案
只需进行一些更改,一切看起来都很好。
menu = ("Happy Ham's Pizza Hut \n\nSIZES \nLarge pizza (20in.) \nMedium pizza
(15in.) \nSmall pizza (personel 10in.)\n")
menu += ("\nTYPES \nVegan pizza(non-dairy cheese, tofu pepporoni)\nVegatarian
pizza(ground-corn crust, graded radish cheese alternative, cucumber
rounds)")
name = str(input(" \n Hello, and welcome to happy ham's pizza hut! \nWould you
like a menu? \n >>"))
acceptableresponses = ["yes", "yes please", "YES!"]
### create a list of acceptable responses to display the menu.
if name in acceptableresponses:
print(menu)
else:
print("Customer doesn't want to see menu")
### else case doesn't show menu rather prints a msg saying user doesn't want to see the menu since user input something other than what's in the list declared.
推荐阅读
- jdbc - postgresql jdbc 非常简单的查询和 56 行,比 psql 的 2x、3x 查询慢
- angular - 如何在 forRoot() 中注入/使用服务
- python - 如何跨时间识别道路几何形状?
- javascript - mapStateToProps 和 mapActionsToProps 未在反应组件中触发
- kotlin - 如何在 Kotlin 中使用反射获取函数引用
- sql - 从 Postgres 表中删除数百万行的自动化方法
- javascript - 创建的图像高度有时不起作用
- excel - 将D列复制到A列Excel VBA
- arm - 无 phy 的 SGMII - Xlinix Zynq UltraScale+ RFSoC 板上的外部环回
- mongodb - Nestes Schema 虚拟