基本if语句无法正常工作

时间:2016-03-27 19:55:50

标签: python if-statement while-loop

我是python的新手,所以不要过于严厉地判断。我已经研究了几个小时,但我还没有得到我想要的。

results = {}
counter = 1
pastabake = "Pastabake recipie:"
pittapizzas = "Pitta Pizzas recipie:"

while True:
    response = input("Which ingredients do you have?");
    results[counter] = response
    counter +=  1
    if counter == 6:
        break
    if response == ('pasta' and 'onion' and 'cheese' and 'garlic'):
        print(pastabake)

发生的事情是,一旦我运行它,只有在我输入最后一个响应时才会打印出来的照片,即大蒜"而不是在打印pastabake之前以任何顺序拍摄所有照片

我不明白我做错了什么? 感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

您正在测试多个变量是否错误,只需将它们放入列表中并检查所需的值是否在列表中:

results = {}
counter = 1
pastabake = "Pastabake recipie:"
pittapizzas = "Pitta Pizzas recipie:"

while True:
    response = input("Which ingredients do you have?");
    results[counter] = response
    counter +=  1
    if counter == 6:
        break
    if response in ['pasta', 'onion', 'cheese', 'garlic']:
        print(pastabake)