Stocking Out pls 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 9 instead of the correct 7" I am getting this error message


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


#2

a function ends the moment a return keyword is reached.

this means, currently, you don't check if item is present in stock, and you don't reduce stock after you bought an item


#3

what is the solution for this error ??


#4

only have a single loop in which you increase total and reduce stock, then after the loop return total


#7

Please write the code to show us how it's done. Am waiting for it please. My code is showing that it's not giving correct figure 7


#8

this is a violation of the guidelines, make a new topic so we can help you


#9

But it's on same stocking out issue! Should we be duplicating post when similar unclosed thread still exist?


#10

if i recommend you to make a new topic, then the answer to this question is: Yes, you should.

Otherwise this topic becomes cluttered, making it much more difficult for other users to find what they need. Making new topic will also be seen by more people


#11

Your loop through the food is being duplicated and doesn't make sense since both loops return the same result. Delete the first 'for loop' and its executables and you should be fine.


#12

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