11. Making a Purchase



Traceback (most recent call last):
File "python", line 24, in
File "python", line 21, in compute_bill
TypeError: unsupported operand type(s) for +=: 'int' and 'dict'

I thought by coding
food = [prices]
prices will reflect the prices within the dictionary.

Replace this line with your code. 
def compute_bill(food):
    total = 0
    for total_price in food:
        total += total_price
    return total
food = [prices] 
print compute_bill(food)


Hello @methodpro18652,

The design of our function is so that ,the parameter food (list of fruits) basically is a list data type.
when we loop over it using total_price , it simply takes string value of fruit's name inside of food list,
so you're adding string with number total, two incompatible data types,You'll get an error. here you need to use this total_price as key of prices (global variable) that will give you access the price of fruit on certain key, then you can add it.

total = total + <use total_price as a key to access price from prices dictionary>


thanks for getting back to me.

I now recognize that I am adding food(list type) to total(int type). What I am still confuse is the relationship between key and value, and how to use dic to access them.


precisely food's item, that you're trying to add to int type total as you defined it as total = 0.

What I am still confuse is the relationship between key and value, and how to use dic to access them.

prices is a dictionary (globally defined It means we can use it anywhere,inside a function,outside a function etc), as You can see it is collection of key and value pairs(name of fruit is key and price corresponding to it is value).
to access value of a certain key, we need to pass key to it..

like this..

prices["orange"] #it will give us price of orange

key point to understand that when we loop over food, our iterator total_price (name is confusing ) gets assigned with item's-value of food at each iteration, the item is nothing but a string (name of fruit ) inside it. (w.r.t to program)
we can pass it to prices and we will have price of that fruit at the moment.
Now as we have price of a fruit in hand,we can add it to total.


I am still not hitting the right mark and I cannot figure why.

Write your code below!

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


It's okay, I will give you a basic skeleton..

1.Define function compute_bill() with parameter food
2.Define a local variable total with initial value of 0.
3.loop over food with some iterator say item.
4. pass the item as key of prices dictionary and add that to total.
5.return the total

Before you attempt this, revise how we access the dictionary values using key.
also take a look at how for loops is used to iterate over lists data structure.

If you find something confusing, point it out,I will explain!


Yes, I coded correctly this time.

I am still a bit unclear on how for loop is used and processed, especially when you mentioned lists type. Can you care to explain it?


Check this thread,


Thank you so much. I am so grateful of your existence T.T


I hope things are a bit clear now, try reading some side tutorials with CC's python course. There are tons of good tutorials over the internet.


Yea, I am trying to absorb as much as possible but the biggest problem is I don't know where I am confused on until I actually encounter them, and when I do, it is hard to find the root of it.


It's natural, keep going.
check this link..

python.org is official site of python, You will find tons of things there.


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