【问题标题】:How to use input statement with with conditions?如何使用带有条件的输入语句?
【发布时间】:2022-11-20 08:28:22
【问题描述】:

想要有一个输入变量来问一个问题,其中包含许多合适的 true 语句,但根据用户输入的内容给出不同的答案。我当前的代码与下面类似,但 niot 工作正常。

我只想打印一个语句或另一个。我还想让这种代码组合适用于许多其他答案,比如“也许你有什么?” “有便宜的吗?” “我想要随机的东西!”

我的代码如下所示:

menu_decision = input('would you like to see the menu?') ####input variable for coustmers decision
if 'yes':
    print('todays menu: ' + combo_1 + combo_2 + combo_3)
elif 'no':
    exit('Have a nice day!')
elif 'what do you have?':
    print('todays menu: ' + combo_1 + combo_2 + combo_3)
elif 'I want something random!"
    randomizer = input('what is your food preference?')
print('combo shack') ##title of my program
combo_1 = 'FOOD1, ' ###variable 1
combo_2 = 'FOOD2, ' ###variable 2
combo_3 = 'FOOD3 ' ####variable 3

menu_decision = input('would you like to see the menu?') ####input variable for coustmers decision
if menu_decision := 'yes': ####if yes print menu with all three  varibles
    print('todays menu: ' + combo_1 + combo_2 + combo_3)
        #cookie_randomizer= input("how many combos would you like?:")

elif menu_decision := 'no': ###if no dismiss coustmer
    exit('Have a nice day!')

【问题讨论】:

    标签: python


    【解决方案1】:

    Python 使用 == 检查字符串条件匹配,例如

    print('combo shack')### title of my program
    combo_1 = 'FOOD1, ' ### variable 1
    combo_2 = 'FOOD2, ' ### variable 2
    combo_3 = 'FOOD3 '  ### variable 3
    
    menu_decision = input('would you like to see the menu?') #### input variable for coustmers decision
    
    if menu_decision == 'yes':
        print('todays menu: ' + combo_1 + combo_2 + combo_3)
    
    elif menu_decision == 'no': 
        exit('Have a nice day!')
    
    elif menu_decision == 'I want something random!':
        randomizer = input('what is your food preference?')
    

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 2021-09-19
      • 2021-05-02
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 2012-06-09
      相关资源
      最近更新 更多