12. Stocking Out


#1

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

where are the wrong?


#2

Please explain what is happening different from what you expect so that we know what to look for. Also take care to get the formatting of your code right, what you posted isn't valid Python code.


#3

I'd like to agree with @ionatan about your formatting. Without it it's almost impossible to know what you've gotten wrong.

Did you check to make sure that your return statement was outside of the for loop?


#4
what's wrong with 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
}

# 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
compute_bill(shopping_list)

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

#6

Hi,

When we say

how does python recognize that the x's are the items in shopping list (or) how does python decide what food is? I am asking since food is not defined before the function. Thanks.


#7

Guys change the stock value for each of the values in the calling function.
Like This
shopping_list = ["banana", "orange", "apple"]
stock = {
"banana": 7,
"apple": 1,
"orange": 33,
"pear": 15
}


#9

thanks, pyywhiz04125, that helped! must have reduced the stock with the first (probably wrong) try.
the codes for the function from betarunner12266 and gmanu are both right.


#10

Yes.....their code also correct...it's just the stock value


#11

I don't understand why we need to change the value of the stock. This exercise needs to check, and not add items that are not in stock to the compute_bill total. Shouldn't we keep the values the same and let the code figure out that there's no apples?
Can you please look at my code and let me know what is wrong? It's giving the the message: "calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7" - Why is this adding pears to my list if Im calling compute_bill for items in my shopping list? Also, how can i subtract the item from stock?

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

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

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

Thank you,


#12

I have the same problem


#13

def compute_bill(shopping_list):
    total = 0
    
    for food in shopping_list:
        if stock[food] > 0:
           total += prices[food]
        return total                 <------ Why?
    return total

You have this extra return statement in your compute_bill function. What are you trying to do with that?


#14

I actually changed the code a bit, and I'm still not able to pass this lesson. Here is what it looks like now:

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

This is what I get: Oops, try again. calling compute_bill with a list containing 2 apples, 2 pears and 7 bananas resulted in 34 instead of the correct 30

This shopping list is a bit vague to me. What am I really trying to compute here? The total amount of bananas, oranges and apples? I am getting so many different answers. Super stuck... Help!


#15

The assignment is something like this: "You go to the store with a shopping list and try to buy everything on it. What price does the receipt say when you're done?" You can't buy something that isn't in stock and you can't buy more items than what are in stock.

I think that the issue here is that you (or well the test code) try to buy 7 bananas when there only are 6 in stock. Because of that it expects to not be able to buy the 7th one. Your code doesn't do anything to handle this situation. You need to remove items from the store as you buy them.


#17

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

Is this the way to handle the inventory? It's giving me the following 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}


Stocking Out - Any thoughts?
#18

Never Mind... This worked! Thank you all for the help!!! Moving on


#19

They expect us not to change the stock value while shopping.It's a problem at their end.So change the stock value according to the calling function.I did the same code in another platform,it worked perfectly without changing stock value.


#20

The problem is that you aren't supposed to call the function after defining it for this assignment, but many do it out of pure reflex or simply because they want to see if it works. So many headaches would have been avoided if the code behind the curtains would reset the stock-list before testing the function. Or an even easier fix; actually tell the user about this in the instructions :stuck_out_tongue:


#21

what worked here? because i really don't see what the error message (i keep getting the same one) means.


#22

I removed the print statement from my code. You are not supposed to call it when you are done with the function. Check the last reply before yours here.
I kept trying to call the function when I was done to see it printed, but I was not supposed to do that.