12. Stocking Out, need help


#1


https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096#

Oops, try again.
calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7


It should remove one item in stock after it has been purchased and add up a price


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


#2

a function ends the moment a return keyword is reached, which in your case happens in the first run of the loop. You might want to change the indent of return so the whole for loop can run


#3

Ok so changing the return to after the print should resolve the issue?

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


#4

at this point in the python course you should know what indent(ion) is? The amount of spacing, which is crucial for python since it doesn't use brackets

No, just no. That is not what i said you should do. Return was at the good line, the indent was just wrong


#5

yeah, it had spacing but for some reason when I pasted it in, it removed the spacing

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

Is this correct? (it autoremoves spacing for some reason when I post this)


#6

you need to use markup for spacing to show

No, now your code is even worse, this code you had:

def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] > 0:
            total = total + prices[item]
            stock[item] = stock[item] - 1
        return total # change indention on this line
print compute_bill(shopping_list)

is almost good. See comment, all you need to do is change the indent on that one line. No need to place return after your function call, that is just silly.


#7

Changed it to this and it worked:

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

but I get a different error,
Oops, try again.
stock doesn't look quite right! Make sure to not call compute_bill
since it changes the stock! It should contain: {'orange': 32, 'pear':
15, 'banana': 6, 'apple': 0}


#8

you are not allowed to call the function, remove this:

print compute_bill(shopping_list)

#9

Thanks, removing this made the code work, sorry for any inconvienience


#10

still get this

calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7


#11

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

AND I STILL GET:
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7


#12

you have the exact same problem? How can you not use the solutions post in this topic?


#13

Hi Guys. Can you please help me out with whats wrong here. Thanks.


#14

a function ends the moment a return keyword is reached, so you return stock[item] while you should return total


#15

Thanks. Its working :slight_smile:


#20

Your patient bro lol


#22

Same error i also face it


#23

Please make a new topic with your code + error message so someone can help, replying that you face the same problem doesn't help


#26