Форум сайта python.su
0
Привет!
Подскажите, пожалуйста, с задачей:
Stocking Out
Now you need your compute_bill function to take the stock/inventory of a particular item into account when computing the cost.
Ultimately, if an item isn't in stock, then it shouldn't be included in the total. You can't buy or sell what you don't have!
Instructions
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.
If the item is in stock and after you add the price to the total, subtract one from the item's stock count.
?
Hint
If you're buying a banana, check if it's in stock (larger than zero). If it's in stock, add the cost of a banana to your bill. Finally, decrement the stock of bananas by one!
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: if stock(item) > 0: total = prices[item] + total return stock[item] - 1 return total
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: ‘dict’ object is not callable
Отредактировано hewonders (Авг. 26, 2016 11:13:49)
Офлайн
61
Покажите как вы вызываете функцию
Офлайн
0
К примеру например:
compute_bill("banana")
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: ‘dict’ object is not callable
Офлайн
1
hewondersstock это же словарь, вроде бы в нем нельзя брать элементы по индексам?return stock[item] - 1
return stock(item) - 1
Отредактировано Stepan_M (Авг. 26, 2016 13:52:33)
Офлайн
0
И при квадратный и при круглых скобках
# Write your code below! def compute_bill(food): total = 0 for item in food: if stock(item) > 0: total = prices[item] + total return stock(item) - 1 return total
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: ‘dict’ object is not callable
Офлайн
61
if stock[item] > 0:
Отредактировано ZerG (Авг. 26, 2016 16:23:26)
Офлайн
0
Имелось в виду в
return stock(item) - 1
if stock[item] > 0:
Traceback (most recent call last):
File “CUsers/fomichevll/AppData/Local/Programs/Python/Python35/ввввввввв.py”, line 25, in <module>
compute_bill(“banana”)
File “CUsers/fomichevll/AppData/Local/Programs/Python/Python35/ввввввввв.py”, line 21, in compute_bill
if stock > 0:
KeyError: ‘b’
File “python”, line 25
compute_bill(“banana”)
^
IndentationError: unindent does not match any outer indentation level
Офлайн
61
# -*- coding: utf-8 -*- stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15 } item = 'pear' # print(stock(item)) # Это не метод словаря - будет ошибка print(stock[item]) #
d = {'a': 'b'}
print(d['a']) b
Офлайн
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
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in -1 instead of the correct 7
Oops, try again. Your code looks a bit off. See the console window for the error. Check the Hint if you need help!а в console window:
Traceback (most recent call last):
File “python”, line 25, in <module>
File “python”, line 21, in compute_bill
KeyError: ‘b’
Отредактировано hewonders (Авг. 29, 2016 10:15:58)
Офлайн
61
Пользуйтесь оператором print() для отладки вашего кода
# -*- coding: utf-8 -*- def compute_bill(food): total = 0 for item in food: print(item) compute_bill('pear')
p
e
a
r
Офлайн