Форум сайта python.su
0
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
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








Отредактировано hewonders (Авг. 29, 2016 11:29:30)
Офлайн
857
def compute_bill(food): total = 0 for item in food: if stock[item] > 0: total += prices[item] stock[item] -= 1 return total
Офлайн
0
Огооо)))
Помогло.
А как понять
total += prices[item]
total = prices[item] + total
total = prices[item] + total
Отредактировано hewonders (Авг. 29, 2016 11:50:49)
Офлайн
857
hewonderstotal += prices[item]
total = total + prices[item]
hewondersКорректно, ты просто с отступами пропёрся.
Разве это некорректно?
Отредактировано py.user.next (Авг. 29, 2016 11:54:06)
Офлайн
0
PS Нашёл решение на другом сайте как раз по логике, которой я следовал:
def compute_bill(food): total = 0 for item in food: if stock[item] > 0: total = total + prices[item] stock[item] -= 1 else: total = total return total
total = total + prices[item]
else: total = total
Отредактировано hewonders (Авг. 29, 2016 11:55:48)
Офлайн
0
Если в стоке есть фрукт и он больше нуля, тогда выполняется условие:
total = total + prices[item] stock[item] -= 1
Отредактировано hewonders (Авг. 29, 2016 12:04:25)
Офлайн
0
Ладно, оставлю как подсказали!! Спасибо всем за помощь))
Офлайн
857
hewondersКто добавлял такое, тот не знает нифига. Масло масляное.
Они только добавили ещёtotal = total
Офлайн
1
hewondersТы точно уверен, что правильно понял суть задачи?
shopping_list =
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:
if stock(item) > 0:
total = prices + total
return stock - 1
return total
shopping_list = ["banana", "orange", "apple"] stock = { "banana": 3, "apple": 0, "orange": 32, "pear": 15 } prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3 } # Write your code below! def compute_bill(foods): total = 0 for i in foods: if stock[i] > 0: total += prices[i] stock[i] -= 1 return total print(compute_bill(shopping_list)) print(stock)
5.5
{'apple': 0, 'orange': 31, 'pear': 15, 'banana': 2}
Отредактировано alalekseev (Авг. 29, 2016 21:07:12)
Офлайн
61
Та уже писали в предложениях что форум неправильно работает - но чото никто не пофиксил!
Офлайн