收到"哎呀,再试一次!" 在"计算法案"codeacademy Python练习

0 python

编写一个函数compute_bill,它将参数food作为输入,并通过循环遍历食物列表并汇总列表中每个项目的成本来计算您的账单.

现在,请继续并忽略您要结算的商品是否有货.

请注意,您的功能应适用于任何食物清单.

给定的代码是

groceries = ["banana", "orange", "apple"]

stock = { "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}

prices = { "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}
Run Code Online (Sandbox Code Playgroud)

我写过:

def compute_bill(food):
    total = 0;
    for f in food:
        if stock[f] > 0:
            total+=prices[f]
            stock[f] -=1

    return total
compute_bill(groceries)
Run Code Online (Sandbox Code Playgroud)

错误信息是

哎呀,再试一次!当['apple']用作输入时,您的代码似乎不起作用 - 它返回0而不是2.

Jar*_*sen 6

现在,请继续并忽略您要结算的商品是否有货.

去掉

  • if stock[f] > 0:

  • stock[f] -= 1