12 Stocking Out


#1

Hello. I've tried this a bunch of different ways and have finally just been copying and pasting from these posts out of desperation. I then try to apply the suggested fixes (e.g. this one had me move the return back two tabs). I would prefer a sample answer that works that I can then analyze when I see it. I don't feel like any more trial and error is worthwhile. Anyone want to directly (no hints) give the/an answer for this one?

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


#2

We cannot tell if your indentation is the problem. When posting code, especially Python, code sample formatting is imperative. Precede the code with three back-ticks, and follow it with three more...

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

The above code looks correct. What error message are you getting?

Update: Tested your code and passed.


#3

Hello friends,
I have written the below code and tried many times,but it says 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 have no clue what might have gone wrong. I request you to look into it, thanks

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

#4

i made the same piece of code as your's and i'm getting this:

File "python", line 22
total += prices[item]
    ^

IndentationError: expected an indented block

I'm realy don't know what i did wrong, overall code looks like this:

   # Write your code below!
def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] > 0:
        total += prices[item]
        stock[item] -= 1
    return total

UPDATE:
I've managed to fix this out on my own, you just need to make 4 spaces before the "total", this is the right answer:

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

#5

Hi @samauden ,

Don't use this statement in Exercise 12: Stocking Out ...

print compute_bill(shopping_list)

Codecademy does not want you to call the compute_bill function yet. Instead, you can use the statement in Exercise 13: Let's Check Out!.


#6

hi @geomaticmashups
I have removed the call function and it's now working, thanks. Sometimes the ouput giving an impression that some thing is wrong as per codeacademy and not from python. I executed the same program on Eclispse IDE and it executed correctly.


#7

Thank you madmorze! Worked like a charm! Do you know why indenting 'total' under 'if stock[item] > 0:' is important conceptually? What does it do to the relation between if statement, if statement's total and stock? Even if you're not sure about that, thanks again. I was really stuck and really wanted to move on. Thanks again and again.


#8

Python defines code blocks with indents, which must be correct for the interpreter to be able to parse the blocks as expected.

def my_method():
    '''
    first line in block
    second line
    '''
    for something ...
        '''
        first line nested block
        next line
        '''
    return

Even the placement of comments should be indented to match the structure they are contained in.