2015-11-12 47 views
0

我不斷收到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

你接近,但你忘了把iteritems()您的發言結束。

變化

for number, key in wings_quantity: 

for number, key in wings_quantity.iteritems(): 

你需要重寫你的打印語句,因爲他們正試圖訪問字典兩次這個問題之後。既然你已經有值你可以打印出來,像這樣:

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. 
+0

好吧,我希望它能打印出「(大小)有(數字)翅膀」我知道格式不對,我正在用ipad打字。我該如何做到這一點?我使用2.7。 – cupilot

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

回答您的評論