11. Making a Purchase


#1

I've been trying for a while to pass 11. Making a purchase but I don't know what's wrong with my code. Could some help me out please!!!!?!?

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


#2

hi your code is fine its maybe a indent problem here how its should be..

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

#3

Thank you it works!!!


#4

Hello, I have two questions:

1) I'm sorry but what does the += mean?

2) Would you be willing to provide some feedback on the following?
def compute_bill(food):
total = 0
for n in food:
d = total + prices[key]
print d


#5

Here's an example to understand it:

total = total + 12

is the same as

total += 12


#6

HI its a way to add something to another value like that
num = num + 1 its can also be that num += 1.

the intructuion say
Define a function compute_bill that takes one argumentfood as input.

def compute_bill(food)

In the function, create a variable totalwith an initial value ofzero.

def compute_bill(food):
    total= 0

For each item in the food list, add the price of that item to total.

def compute_bill(food):
    total= 0
    for key in food:
        total = total + price[key]

instead of key you can choose any other word ......and price should be add to the total with the word you choose in the for loop. in my case key

and then you should return the total

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

#7

Thank you frozen T, much appreciated.


#8

Hi Wiz, thank you for the feedback! messed around with the code some more and it seems that having key in the for-loop is necessary for the code to work, though I don't understand why. I thought that compute_bill was a list and would not have keys as a dictionary would have....

I got the code to work so thank you for your help!


#9

jeez, thanks a lot :slightly_smiling:


#10

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


#11

What's the question? Does it not work? Website giving errors? Please write it there so we can actually help you instead of a code out of nothing.. :wink:


#12

That's the answer :slightly_smiling: