2013-10-08 161 views
0

我正在從Code Academy進行練習以遍歷shopping_list。 爲什麼下面的代碼結果在結果中返回一個額外的None爲什麼for循環返回None?

shopping_list = ["banana","apple"] 

stock = { "banana": 6, 
    "apple": 0, 
    "orange": 32, 
    "pear": 15 
} 

prices = { "banana": 4, 
    "apple": 2, 
    "orange": 1.5, 
    "pear": 3 
} 

# Write your code below! 
def compute_bill(food): 
    total = 0 
    for x in food: 
     print x 
     total += prices[x] 
compute_bill(shopping_list) 
+7

你的函數將返回'None'。也許你的環境只是在函數運行後顯示返回值 –

+0

#在下面寫下你的代碼! DEF compute_bill(食品): 總= 0 用於食品中X: 打印X 總+ =價格[X] 返回總 打印 「總:」 + STR(compute_bill(和shopping_list)) – ChuckCottrill

+0

第二示例後本教程的這一部分 - 。 – wwii

回答

2
shopping_list = ["banana","apple"] 

stock = { "banana": 6, 
    "apple": 0, 
    "orange": 32, 
    "pear": 15 
} 

prices = { "banana": 4, 
    "apple": 2, 
    "orange": 1.5, 
    "pear": 3 
} 

# Write your code below! 
def compute_bill(food): 
    total = 0 
    for x in food: 
     print x 
     total += prices[x] 
    return total 
print(compute_bill(shopping_list)) 

您需要使用return語句獲取功能compute_bill的結果。