11. making a purchase


#1

this is my code:

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

I'm not sure why it isn't working


#2

what s? its a product (could be apple, banana, depends on what is in the food list)

so we need to use s as key to get the price of the the product from the prices dictionary


#3

Im not sure if you phrased it wrong or if im confused, can you clarify what you meant


#4

here:

total += [s]

tell me, what is s? what does the variable s contain? Does it make sense to add this to total


#5

it contains the letter s, representing the pricing codes line


#6

are you sure about that, take a look:

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 s in food:
        print s

compute_bill(shopping_list)

go ahead and run this code to see what in the s variable.

Then ask yourself again: does it make sense to add s directly to total?


#7

okay, by the error I can tell that the s is not a representation in the code but im not sure what is meant to be there instead


#8

this code was purely to demonstrate showing what s contains. It contains products (apple, banana and so on), did you see?

What do we want to add to total? the price of the product (the price of an apple for example), so how do we get from product to price? Maybe our prices dictionary can help us with this?

we can use s as key to associated price value from the prices dictionary


#9

okay, so s represents the items but we have to connect s to the prices of the items


#10

woohoo :slight_smile: very good

Yes, for this we have our prices dictionary, how do you think we can achieve this? the square brackets you had:

[s]

where i good start


#11

so firstly I should put brackets around the s, then add a kind of key function before the brackets


#12

not really, look how we can get a value from a dictionary:

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

i now use apple as key, what is your key?


#13

would the key be s and then I place prices infront of s


#14

yes, very good :slight_smile:


#15

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 s in food:
print s

compute_bill(shopping_list)

it still says error at the bottom for some reason


#16

you need to get back to this code:

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

That code was purely to show what the variable s contained

but remember what is in s, and how we can use this as key to get the price from the prices dictionary


#18

it still says there was an indenation error but I cant see the aroow signifying where


#19

can i see your full code again?


#20

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

got some help and got this, this works well but im not sure how


#21

we just talked about in detail how this worked? :frowning: