Stocking Out


#1

Is there any thing wrong in my code.

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

return total

#2

In the stock line, you are modifying "stock", but you really want to modify "stock[key]" (the amount of one of the items given by key).

You can put "stock[key]" on both sides of the "="


#3

hi.
i have a doubt. which is key and value.

For example:
fruits = {
" apple" : 2
"banana" :3
"pineapple" : 4

}

In the above dictionary "value" "apple", "banana", "pineapple:
and "key" 2,3,4.

Am i correct.


#4

Sorry it is the opposite there.

Key means "apple", value means 2.

Edit: "key" is like "index" in a list or array if you are familiar with those.


#5

for entire code in python is this resembles same.


#6

This is my code:

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

It's return the following error:
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: string indices must be integers, not dict


#7

you want stock[item] not item[stock]

edit: also careful, just subtract 1, not stock[item]


#9

Write your code below!

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


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.