11. Making a purchase: 1.5?


#1

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

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"orange": 1.5,
"apple": 2,
"pear": 3
}

Write your code below!

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

So for whatever reason, when this code runs it return 1.5 for apple instead of 2 like it's supposed to. Any Ideas?


#2

The keys in prices are strings, not lists.

for item in food:
    total += prices[item]

We cannot use our parameter name (which refers to a list) as the iterator variable in our loop, else it destroys the list and we end with nothing happening.


#3

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

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"orange": 1.5,
"apple": 2,
"pear": 3
}

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

Write your code below!

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

ok, now there's thiscompute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5, so it goes through the first "check" (the check for apples) fine but now it won't pass the 2nd


#4

Remove this entire segment. The one below it is correct.


#5

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