Stocking out


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

What is wrong with my code it keeps saying I get the wrong total value?


#2

Why are you subtracting from the total cost when the stock hits 0? :smile:


#3

Use this it's working

def compute_bill(food):
total = 0
for item in food:
    if stock[item] > 0:  
         total += prices[item]
         stock[item] -= 1
    else:
        print "You Can`t buy/sell what you don`t have!"
return total

#4

no ghadeer that produces the error of expecting an indented block at total. I was stuck on this one as well. I even copied ghadeers example. It originally failed but once I removed and then redid the indentation fresh it went through for me.


#5

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}

this is the error that i a, getting while using the above mentioned code given by ghadeer. How do i improve?


#6

Hi everybody!

Same thing as nitishvij have.
Sure, it is not a bug? Can you fix it?
my function is:
def compute_bill(food):
total=0
for i in food:
if stock[i]>0:
total=total+prices[i]
stock[i]=stock[i]-1
return total
So it's must be normal: I subtract one and total count is changed.
Or I it's not right?
Anyway thanks for this excellent course!
With respect and waiting for response.

P.S.
2 nitishvij
Maybe interesting for you.
I fix the problem, using "dirty hack" - change 'orange' to 33 and 'banana' to 7.


#7

it must be a bug. I have do it the same as you but failed, then i try another method, error also. I am going crazy.


#8

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

just now I pass it with this code.