Why is this wrong? ... Python


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


#2

x is a string, not an integer, and therefore an invalid index. It is a key in the prices dictionary, though.


#3

so how to correct it.


#4

This is the list that is passed to the function and referred locally as food. Notice that "banana’, ‘orange’ and ‘apple’ are all keys of the prices and stock dictionaries?

for x in food:

x is one of those keys. How do we access a dictionary item given a key?


#5

still could not solve it


#6

How do we access a dictionary item given a key?


#7

dont remember, thats why i’m asking.


#8

ok now i get it thanks for helping


#9

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