Lesson 12. Stocking Out


#1

Hi everybody.

"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'm having a problem to solve this lesson because when I click to compile, a message above shows.

Look 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:
            total += prices[key]
            stock[key] = stock[key] - 1
    return total

print compute_bill(shopping_list)

Anyone can help me?


#2

Your code compiles, but maybe the lesson checker software doesn't want you to compute the bill, because it is like your are "checking out" of the store and the store's inventory (stock) will change and then in the following lessons the counts will be off.


#3

Yes, was that.
I commented the function call and it works!

Tks!


#5

Dear Friend! I would appreciate a piece of advice on the following
I still get that error about not calling compute_bill function having this code and indentations:

Write your code below!

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

P.S. Oops, I see the comments do not save my indentations.
I have indentation before total=0, next one is before IF, next for conditions(total and stock), return is inline with for, not if....


#6

so u got your output rit??


#7

Nope...I still get this: 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 must be missing smth too obvious...
My exact code again, but I added the comments where indentations go only here, not in codeacademy lesson page

Write your code below!

def compute_bill(food):
#indentation
total=0
for key in food:

indentation

    if stock[key]>0:

indentation

       total+=prices[key]
       stock[key]-=1

next line is same as "for " indentation

return total

#8

do one thing first replace with my code try and then try your own code


#9

Thank you, I had all the same indentations as you have, only key instead of fruit and had no spaces before -= and +- operators.
I used your code, got same error, then mine(almost the same),got it again:

I'd show both with that error window, but it does not move and hides the code...

I see the person , who initiated the topic had calling print compute_bill at the end. I do not... Very strange... I bet it will be smth too simple/
Thank you for helping me, I'll keep investigating those several lines...


#10

Indeed, it WAS smth stupid and little! My OWN action before coding:
I changed manually:
"apple": 10 from "apple": 0, thinking that if 1 was subtracted from a positive number, it would be more visual for the exercise.
I did not think that the lesson holds to its initial values to this extend assuming that it was allowed....
So I assumed it would give me 10-9 apples!
My code did not actually change stock, I did it manually at the very beginning and forgot it.
I have mixed feelings. Another proof that one should follow the instructions carefully.
On the bright side, I read numerous posts on this topic here and on calling functions in python online.


#11

Hey, your code was right, mine too=)
Actually, I did a stupid, non-instructed thing to initial data, thinking that it was allowed (changed apples from 0 to 10).
Thank you for helping me)


#12

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

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

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

print compute_bill(prices)

This my code! can you review??!


#13

Hi!
Yeah, it is ok, but remove this :

print compute_bill(prices)

You are calling a function there, which is not needed. Your last line should be :

return total


#14

Ohh it's working!!!
Thank you !!)


#15

You are very welcome)


#16

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