3 回答

TA貢獻(xiàn)1906條經(jīng)驗 獲得超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貢獻(xiàn)1802條經(jīng)驗 獲得超10個贊
看起來每個值都是一個列表(盡管它可能應(yīng)該是一個元組):
itemname, qty, eachprice
所以它應(yīng)該很容易迭代并直接求和:
sum(qty*eachprice for _, qty, eachprice in inventory.values())

TA貢獻(xiàn)1799條經(jīng)驗 獲得超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)
添加回答
舉報