0

我正在 Code Academy 进行一项练习,以遍历shopping_list. 为什么下面的代码结果会在结果中返回一个额外None的?

shopping_list = ["banana","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 x in food:
        print x
        total += prices[x]
compute_bill(shopping_list)
4

1 回答 1

2
shopping_list = ["banana","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 x in food:
        print x
        total += prices[x]
    return total
print(compute_bill(shopping_list))

您需要使用return语句来获取函数的结果compute_bill

于 2013-10-08T04:38:10.767 回答