Codecademy-晚餐市场的一天:放养

时间:2015-12-03 04:40:05

标签: python python-2.7

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

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

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    for hi in food:
        if stock[hi] > 0:
            total = total + prices[hi]
            stock[i] = stock[i] - 1

    return total

Codecademy解释器返回此声明“使用包含2个梨的列表调用compute_bill,1个橙色和8个香蕉导致39.5而不是正确的31.5”。 我不明白使脚本运行不正确的脚本有什么问题。 如果能够解释一下compute_bill(食品)如何将自己与shopping_list,股票和价格联系起来?

1 个答案:

答案 0 :(得分:-1)

此代码是正确的:

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

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

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    for key in food:
        if (stock[key] > 0):
            total += prices[key]
            stock[key] = stock[key] - 1

    return total

print compute_bill(shopping_list)  
stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}