Help Me Identify the mistake


#1


A Day at the Supermarket
12. Stocking Out

Error Message:
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}

I get this error message whereas I believe the code is correct(there is a comment in the end #for checking)


shopping_list = ["banana", "orange", "apple","pear"]

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 x in food:
        if stock[x]>0:
            total+=prices[x]
            stock[x]=stock[x]-1
    return total
print compute_bill(shopping_list)

#for checking
for x in stock:
    print ("%s %d")%(x,stock[x])


#2

remove this line:

print compute_bill(shopping_list)

it will execute the function which will change the stock


#3

yeah..removing the code actually solves the problem but in the code compute_bill is not called and the logic asked in the question is in the compute_bill() function
link :https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096#


#4

yes, but if the function is called, the stock changes, which makes validating really difficult because you don't know what the stock is

so you can have the function call until you reached the right code, then you temporary have to remove it to get pass the exercise


#5

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