"Stocking Out" Please, can you explain the logic behind it?


#1



So, I've seen all the correct code for this exercise, so I know what to write.
I've also looked at this video on youtube

But I cannot advance unless I understand the logic behind it, wich I don't. Here is what I can't understand:

  1. We have to define a function that will automatically compute our bill. As a customer?

    def compute_bill(food):

  2. Obviously our total starts at zero

    total = 0

  3. From what I understand, as a customer I buy the three items listed at the top. (see image)

So i'm buying one banana, one orange and one apple. Right??

  1. Here is where I get lost.
    for i in food:

How does the code knows what food I need. There's no reference to shopping_list???

  1. How come the result stays the same, when i change "i" to "item" or to any other word. I don't understand.

As you can see, I'm super newb, be gentle.

Thanks,

Carlo


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


#2

we are customers? i don't think so:

Congratulations! You are now the proud owner of your very own Codecademy brand supermarket (6. your own supermarket)

you can pass the shopping_list as as argument on the function call:

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

food serves as a placeholder until you call the function and supply the argument. The advantage of this approach is that if another customer has a different shopping_list, you can simply call the function again with a different argument (customer 2 shopping_list)

because here:

for i in food:

you define the iterator in the loop, so you can call it anything you like.


Stocking out
#3

we are customers? i don't think so:

Shopping at the Market
Great work! Now we're going to take a step back from the management side and take a look through the eyes of the shopper.

Thats exercise 10 -13 in the chapter Shopping Trip

so why not just leave the brackets empty. Say def compute_bill() ?

Thanks for yor help


#4

doesn't make us customers, we take the point of view of the shopper to see how we can improve there experience.

because then we can't call the function with the customers shopping_list, and the next customers shopping list and so on

we need the parameter if we want to supply an argument on function call


#5

it's getting clearer now thanks to you.

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

Can you tell me what I'm doing wrong here?


#6

look what stock[item] will give you:

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

for item in shopping_list:
    print stock[item]

i added a loop outside the function purely for demonstration purpose. see what stock[item] gives, and now look at your if condition


#7

A post was split to a new topic: Stocking out


#8

Think I'm getting to where you are directing me:

A) I changed "for item in food' to "for key in food"
B) changed "if item in stock[item] > 0" to "if item in stock >0"

C) still need to work out how to adjust the stock after

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

#9

okay, that was not really the point i was trying to make.

The point i was trying to make was that if stock[item] gives you how many of that item are left in stock, so you can simply check if that is greater then zero:

if stock[item] > 0:

that you don't need item in, i was hoping you would see that


#10

this one is for you stetim94:

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

#11

that looks right :slight_smile: it works?


#12

Yes !

Thank you sooooo much


#13

you're welcome


#14

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