亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 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 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號