我已经开始在Codecademy用Python编程,我遇到了一个问题:
对compute_bill函数进行以下更改:
我的代码是:
shopping_list = ["banana", "orange", "apple"]
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
def compute_bill(food):
total = 0
for each in food:
if stock[each] > 0:
total += prices[each]
stock[each] -= 1
return total
food = ["banana", "orange", "apple"]
compute_bill(food)
我有这个错误
哎呀,再试一次。股票看起来不太对劲!请务必不要致电
compute_bill
,因为它会更改库存!它应该包含:{'orange':32,'pear':15,'banana':6,'apple':0}
我不明白为什么会有问题。
答案 0 :(得分:1)
消息说
请务必不要致电
compute_bill
...
但您的代码包括:
compute_bill(food)
删除该行。
答案 1 :(得分:0)
你只需要做的就是摆脱它。如果你想检查,这是我的代码。我已经完成了Codecademy。
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 item in food:
return total
total += prices[item]
根据您的代码判断,它完全相同。
答案 2 :(得分:0)
def compute_bill(food):
total = 0
for each in food:
if stock[each] > 0:
total = prices[each] + total
stock[each] -= 1
return total
答案 3 :(得分:-1)
告知调用函数中每个值的股票值。 喜欢这个
shopping_list = ["banana", "orange", "apple"]
stock = {
"banana": 7,
"apple": 1,
"orange": 33,
"pear": 15
}