12-Stocking out


#1
shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below! total = 0
for f in stock: 
    total = 0
    if stock[f] > 0:
        total += prices[f]
        stock[f] = stock[f]-1 


It returns 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}.Somebody help me please.

#2

Please Re-edit your Post,
by selecting all the code,
and then =click= on the </>-symbol-of-this-editor,
your code will then be in a pre-code mode
and you will be able to make the proper indentations.....


#3

please help.I make the correction


#4

And where is the definition of your compute_bill function ???
as is asked in the instructions
Make the following changes to your compute_bill function:
[/quote]


#5

I am getting the same error as the original post.
I have run & tested the code several time in eclipse, with different variables, all working as expected.
However, when executed within codecademy, I get an error.

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}

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

compute_bill(shopping_list)

If you add 'print stock' to the function, right after print total, the stock quantity for a given item, is reduced by 1

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

output:

5.5
{'orange': 31, 'pear': 15, 'banana': 5, 'apple': 0}

#6

I just found the answer, from another thread.
if you simply remove the last line " compute_bill(shopping_list), the coding session is solved.
while I am happy to be able to move on, I still don't understand why this solves the exercise.


#7
shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below! total = 0
def compute_bill(food):
    total = 0
    for x in food:
        if stock[x] > 0:
            total += prices[x]
            stock[x] -= 1
     it returns Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in None instead of the correct 7.Please help me with the correct code.`indent preformatted text by 4 spaces`

#8

Maybe printing the total will help ???

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

#9

Hi!

I had the same problem. It looks like CA is running our scripts and then checking the value of stock at the end. If You compute an instance of the function, then the values of stock get reduced and that's not the correct value which the CA analyzer is expecting.

Regards,
Lukasz.