2014-04-21 46 views
0

我在Codecademy網站在Python開始編程,我有一個問題:Codecademy網站超市「減」錯誤

進行以下修改您的compute_bill功能:

  1. 當你遍歷每個項目食物,只有當物品的庫存量大於零時,纔會將物品的價格加總。
  2. 如果該項目是在股票和添加價格總後,減去一個從項目的股票數「

我的代碼讀取:

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

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

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

def compute_bill(food): 
    total = 0 
    for each in food: 
     if stock[each] > 0: 
      total += prices[each] 
      stock[each] -= 1 
    return total 

food = ["banana", "orange", "apple"] 
compute_bill(food) 

我有這樣的錯誤

哎呀,再試一次股票看起來不太正確!請務必不要調用compute_bill因爲它改變股票!它應該包含:{'orange':32,'pear':15,'banana' :6,'apple':0}

我不明白爲什麼會出現問題。

+1

「請務必不要調用'compute_bill',因爲它會更改庫存!」 – CoryKramer

回答

1

有消息稱

確保不叫compute_bill ...

但是你的代碼包括:

compute_bill(food) 

刪除了這一行。

-1

Guys調用函數中每個值的庫存值。 喜歡此信息

shopping_list = ["banana", "orange", "apple"] 
stock = { 
"banana": 7, 
"apple": 1, 
"orange": 33, 
"pear": 15 
} 
0

你只需要做的就是擺脫它。如果你想檢查,這是我的代碼。我已經完成了Codecademy。

shopping_list = ["banana", "orange", "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 item in food: 
    return total 
     total += prices[item] 

從您的代碼判斷,它是完全一樣的。

0
def compute_bill(food): 
    total = 0 
    for each in food: 
     if stock[each] > 0: 
      total = prices[each] + total 
      stock[each] -= 1 
    return total