Stocking out's input?


#1


https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096

The code I have is right. I am trying to figure out what the input list, food, would look like!


It's said to be a list such as, food= ["banana", "orange", "apples"]. My question is,it has to have numbers like 9 bananas, 10 oranges etc.right? Shouldn't food be a dictionary then, instead of a list? So something like: food={"bananas": 9, "oranges": 10}


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


#2

Your food parameter takes in a list of keys. As you can see, the keys are common in both the prices and stock dictionaries. Take a look at shopping_list on the first line as an example argument to be supplied to the function. It is a list of keys.

That would be the stock dictionary in your code.

compute_bill computes the bill for a list of fruits. You can access each of the fruit's stock and price by accessing the respective dictionaries...and that's what you are doing in your for loop (stock[i], prices[i]).


#3

Hi @mou_kf,

Your suggestion for a dict of food items to purchase instead of a list of those items is a good idea. However, for this series of exercises, it needs to be a list. With this scheme, if you wanted to buy three bananas, you would need to have three occurrences of "banana" in the list.

@datfatcat's post does apply. Your suggestion would constitute a revised version of what is specified here. You could experiment with your design outside the venue of these exercises.


#4

Thanks! Makes a lot more sense now.


#5

Oh! I must be slightly blind today. I see what they mean now (buying multiple fruits at once eg. shopping_list2 = ['apple','apple','orange']). :dizzy_face:


#6

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