Python 2.7错误 - 无法通过错误

时间:2015-11-12 04:22:07

标签: python-2.7

我一直得到

  

"要解压的变量太多"

错误。任何人都可以帮助我解决这个问题,并可能给我一个解释吗?

wings_quantity = {
    'small' : 8,
    'medium' : 14,
    'large' : 20,
    'half bucket' : 30,
    'bucket' : 65,
    }

wings_price = {
    'small' : 5.99,
    'medium' :8.50,
    'large' : 14.00,
    'half bucket' :20.00,
    'bucket' : 55.00
    }

for number, key in wings_quantity:
    print "  "
    print "There are "+(str(wings_quantity[number]))+ "  wings in a "+(wings_quantity[key])+" size." 
    print " "

for number, key in wings_quantity:
    ppw = wings_quantity[number] / wings_price[number]
    print ('The cost per wing in a %s size is $') + ppw  %wing_quantity[key]

2 个答案:

答案 0 :(得分:2)

你已经离我很近了,但你忘记把iteritems()放在for语句的末尾。

更改

for number, key in wings_quantity:

for number, key in wings_quantity.iteritems():

在那个问题之后,你需要重写你的print语句,因为他们试图访问字典两次。由于您已经拥有了这些值,您可以像这样打印它们:

print "There are "+ key + "  wings in a "+ str(value) +" size."

我在3.4中对此进行了测试,但是在3.x中您需要将其更改为

for number, key in wings_quantity.items():

这为第一个循环产生了这个输出

There are bucket  wings in a 65 size.
There are small  wings in a 8 size.
There are medium  wings in a 14 size.
There are half bucket  wings in a 30 size.
There are large  wings in a 20 size.

答案 1 :(得分:0)

for size,number in wings_quantity.items():
    print "  "
    print "There are "+ str(number)+ "  wings in a "+size+" size." 
    print " "

回答你的评论