【发布时间】:2015-03-28 00:53:32
【问题描述】:
我想创建一个 for 循环,它可以遍历两个字典,进行计算并打印结果。这是代码:
price = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
inventory = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
for k, v in price, inventory:
total = total + price*inventory
print total
我想知道如果我卖掉这家“商店”里的每件商品,我能赚多少钱。我已经检查了here,但对我来说没有用。
错误信息是这样的:
Traceback (most recent call last):
File "python", line 15, in <module>
ValueError: too many values to unpack
第 15 行是 for 循环开始的地方。 我不知道我是否在考虑如何以正确的方式做到这一点。
【问题讨论】:
-
嘿,你使用的是 python 2 还是 python 3?
-
我正在使用 Python 2,但如果我能回答这两个版本,那就太好了
标签: python loops for-loop dictionary