下面的python代码没有给出任何输出

下面的python代码没有给出任何输出,python,python-3.x,list,printing,operators,Python,Python 3.x,List,Printing,Operators,这应该打印出来 那不勒斯人 但是终端中没有输出 你能帮帮我吗?当“巧克力”在“配料”中时,你的if/else块结束,因为条件结束。为了达成其他声明: ingredients = ["chocolate", "vanilla", "strawberry"] if "chocolate" in ingredients: if "marshmallow" in ingredients:

这应该打印出来

那不勒斯人

但是终端中没有输出

你能帮帮我吗?

当“巧克力”在“配料”中时,你的if/else块结束,因为条件结束。为了达成其他声明:

ingredients = ["chocolate", "vanilla", "strawberry"]

if "chocolate" in ingredients:
    if "marshmallow" in ingredients:
        print("Rocky Road")
elif "vanilla" and "strawberry" in ingredients:
    print("Neapolitan")
试试这个

if "chocolate" in ingredients:

    if "marshmallow" in ingredients:

        print("Rocky Road")

if "vanilla" and "strawberry" in ingredients:

    print("Neapolitan")


您误解了
运算符的工作方式您误解了
elif
条件的工作方式请注意
的优先级高于
中的
<代码>“香草”和“草莓”=“草莓”
;所以这一行只测试配料中的“草莓”。
if "chocolate" in ingredients and "marshmallow" in ingredients:

    print("Rocky Road")

if "vanilla" in ingredients and "strawberry" in ingredients:

    print("Neapolitan")
if "chocolate" in ingredients:
    if "marshmallow" in ingredients:
        print("Rocky Road")

if "vanilla" in ingredients:
    if "strawberry" in ingredients:
        print("Neapolitan")