Making a purchase


#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

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

My code is working fine but i dont understand why 'prices' replaces 'food'. Does anyone understands?


#2

i have been struggling with this for a couple hours.
The issue with this site is that they have hidden values or unexplained values.
Like where is 'food' values coming from. its not really explained though you can guess its coming from shopping list.
because its comes from the shopping_list list, the keys that are in Food (essentially shopping_list) go into your 'for' variable (which in your case is 'number'). When the key passes through the variable ('number') it goes into where prices[value] is being declared. Once its in there the function grabs the price or value from the dictionary. Then takes whatever key and its value that passed in there and adds it to the total.

Don't even know if that makes sense but thats how i understand it works.


#3

ok.
Thanks bro for your suggestion


#4

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