为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 if 语句响应多个不同的字符串选项

如何使用 if 语句响应多个不同的字符串选项

潇湘沐 2022-08-11 17:00:12
我正在用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语句时,我希望它对几个不同的答案具有相同的响应。我该怎么做?
查看完整描述

2 回答

?
守候你守候我

TA贡献1802条经验 获得超10个赞

这是整个固定代码


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 in ('yes', 'yes please'):

   print(menu)

else:

   print("Customer doesn't want to see menu")


查看完整回答
反对 回复 2022-08-11
?
撒科打诨

TA贡献1934条经验 获得超2个赞

只是几个变化,一切看起来都很好。


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.



查看完整回答
反对 回复 2022-08-11
  • 2 回答
  • 0 关注
  • 104 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信