3 回答

TA贡献1906条经验 获得超3个赞
Return 和 total 行错位。这将返回 650。
inventory = {847502: ['APPLES 1LB', 2, 50],
847283: ['OLIVE OIL', 1, 100], 839529: ['TOMATOS 1LB', 4, 25],
483946: ['MILK 1/2G', 2, 50], 493402: ['FLOUR 5LB', 2, 50],
485034: ['BELL PEPPERS 1LB', 3, 50]
}
def total_and_number(dict):
total = 0
for values in dict.values():
total += values[1]*values[2]
return(total)
total_and_number(inventory)

TA贡献1802条经验 获得超10个赞
看起来每个值都是一个列表(尽管它可能应该是一个元组):
itemname, qty, eachprice
所以它应该很容易迭代并直接求和:
sum(qty*eachprice for _, qty, eachprice in inventory.values())

TA贡献1799条经验 获得超8个赞
用:
def total_and_number(d):
tot = 0
for k, v in d.items():
tot += v[1]*v[2]
return tot
total_and_number(inventory)
添加回答
举报