def compute_bill(food): total = 0 for item in food: if (stock[item]) > 0: total = (prices[item]) + total return (stock[item]) - 1 return total compute_bill("pear") print(item)
Oops, try again. Your code looks a bit off. See the console window for the error. Check the Hint if you need help!в консоли:
Traceback (most recent call last):
File “python”, line 25, in <module>
File “python”, line 21, in compute_bill
KeyError: ‘p’
Может я не так понял саму задачу?
Есть дефолтный код:
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: total = prices[item] + total return total
Задача 1:
Make the following changes to your compute_bill function:
While you loop through each item of food, only add the price of the item to total if the item's stock count is greater than zero.
В моём понимании код выше меняется на:
def compute_bill(food): total = 0 for item in food: if (stock[item]) > 0: total = prices[item] + total return total
Вторая задача со сведенным кодом первой задачи:
If the item is in stock and after you add the price to the total, subtract one from the item's stock count.
def compute_bill(food): total = 0 for item in food: if (stock[item]) > 0: total = prices[item] + total (stock[item]) -= 1 return total print(item) compute_bill("banana")
консольная ошибка:
File "python", line 22 total = prices[item] + total ^ IndentationError: expected an indented block
блииин я тупооой







