语法错误在其次,这个代码的逻辑就有问题.
compute_bill函数中,food是带入的参数.
total += food[a] , food[a]并不是指价格啊,参数apple带入的话,变成 apple[apple]了.
把函数参数由list类型改成元组类型就ok了,list遍历取出来的是list索引号,就是1、2、3之类的:
compute_bill(('apple'))
# -*- coding: utf-8 -*-
__author__ = 'lpe234'
__date__ = '2015-03-22'
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 a in food:
total += prices.get(a, 0)
return total
print compute_bill(shopping_list)
# Write your code below!
def compute_bill(food):
total = 0
for a in food:
total = total + prices[a]
return total
def compute_bill(food):
total = 0
for key in food:
total = prices[key] + total
return total
print compute_bill(shopping_list)