Not understood how stocking out code works


#1



can someone help me to understand how the below code works


Replace this line with your 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
}
def compute_bill(food):
    total=0
    for item in food:
        if stock[item]>0:
            total+=prices[item]
            stock[item]-=1
            
        
            
        
        
        
    return total


#2

what do you understand already? It is easier to explain something if you know what some knows (or doesn't know)


#3

I know the code till last second statement I don't know how last statement works


#4

which one? this one:

stock[item]-=1

using references words is vague.

well stock is a dictionary containing your products (dictionary keys) and how much there is left in stock (values of key), item is your loop iterator which contains your products the consumer wants to buy, you use this key to access the dictionary and reduce the value by one


#5

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