Stocking Out 12/13 python help


#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
if stock[i] > 0:
for i in food:
total = total + prices[i]
stock[i] = stock[i] - 1
return total


#2

@cloudrunner04788,

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 i in food:
    if stock[i] > 0:
        total = total + prices[i]
        stock[i] = stock[i] - 1
 return total

print compute_bill(shopping_list)

#3

@cloudrunner04788,

 stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
 }
prices = {
   "banana": 4,
   "apple": 2,
   "orange": 1.5,
   "pear": 3
}

Each dictionary ( also called object )
has one or more key-value-pair's ( also called properties)
which are separated from each-other by a comma-,

The prices dictionary / object
has 4 properties
a banana property with property-key banana and its associated number Value 4
an apple property with property-key apple and its associated number Value 2
an orange property with property-key orange and its associated number Value 1.5
a pear property with property-key pear and its associated number Value 3


for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for x in prices:
will lead to 4 iteration's
iteration-1 var x = "banana";
iteration-2 var x = "apple";
iteration-3 var x = "orange";
iteration-4 var x = "pear";
As they want you to display the the associated VALUE of the propery-key you would use
print prices[x]

If they wanted you to _Display the property-key and NOT it's associated VALUE
you would use
print x


As the stock dictionary / object uses the same property-key's
you can also use
print stock[x]