Stuck


#1



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:
        total+= prices[food]
    return total    


INSTRUCTIONS:

Define a function compute_bill that takes one argument food as input.
In the function, create a variable total with an initial value of zero.
For each item in the food list, add the price of that item to total.
Finally, return the total.


#2

Did you mean to write,

prices[item]

?


#3

hehehehe my fault :slight_smile:


#4

opping_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]

    else:

Make the following changes to your compute_bill function:

While you loop through each item of food, only add the price of the item to total if the item's stock count is greater than zero.
If the item is in stock and after you add the price to the total, subtract one from the item's stock count.

HOW CAN I SUSTRACT ONE FROM THE STOCK LIST WHEN THE IF STATEMENT GETS MENTIONED?


#5

You won't be needing an else branch.

total += prices[item]
stock[item] -= 1

#6

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