12. Stocking Out


#1

What's wrong with my code?
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 key in food:
if stock[key] > 0:
stock[key] - 1
total = prices[key] + total
return total

When I press save and coninue I get: Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 4 instead of the correct 7


#2

Hi try to move stock[key] after total = prices[key] + total


#3

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


did this and threw off errors
also how do i indent my code on the forums


#4

the error it threw off was:
Oops, try again. calling compute_bill with a list containing 2 apples, 3 pears and 8 bananas resulted in 41 instead of the correct 33


#5

Hi here the Right indent and that stock[item]-1 should be like that stock[item] -= 1

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

#6

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


#7

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

I don't understand what's wrong with my code. Indenting should be correct. Errors seem to be like everyone else's


#8

sorry but that gives me an indent error


#9

Try it again I made made some changes.


#10

@nickbailey: Your code looks correct to me. Can you post your exact error message?


#11

I figured it out, it was a bug with codecademy, refreshed the code and worked fine


#12

Wizmarco, my code was EXACTLY like yours, but I was getting an error message because I called the function! Deleted that, and bingo!


#13

Could someone explain why we shouldn't call the function? I called it as well and got the error.
I thought calling the function was the whole point? I'm confused.


#15


this should be the correct one


#16

Hit the </> key in the editor, to indent your selected piece of code.

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

#17

2 posts were split to a new topic: 12. I was struggling a lot with this one too


#18