11. Making a Purchase


#1




Hello all, I would be greatful if you could help me correct my coding for this exercise. I have put:

def compute_bill(food):
total = 0
for item in food:
total += item
return total

and I get the error message:
Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'str'

Thankyou in advance for your help!


Replace this line with your code.


#2

well, here:

total += number

you add number to total, but where is number defined?


#3

Ahh yes I realised that straight after posting. Now I have:

def compute_bill(food):
total = 0
for item in food:
total += item
return total

and I get the error message:
Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'str'


#4

so item contains a product (apple, pear and so on), that is not really a price is it? maybe we need to use our prices dictionary? So we can get from product to price


#5

Like this?

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

#6

no. Does that make sense to do? Now you are trying to add a dictionary to a integer.

What is stored in the prices dictionary? the product (as key) and there price as value.

We just determined that item contains our product, so we can use this as key to get the price value from the prices dictionary?

I am helping you, but you have still to think if what you do, makes sense


#7

I'm sorry I am new to coding and have spent about an hour tweaking this in every which way and I still can't get it to work. I completely don't understand. Can you just give me the solution so i can just get passed it and continue learning?


#8

This is part of your learning process.

so we build a loop:

for item in food:

To get all items the customer wants to buy. Given item is a product, we can't add this directly to total. We need to get from product to price.

so we have our dictionary prices. A dictionary has keys and values. the prices dictionary has products as key, and how must they cost as value.

this is very useful, because with dictionary's we can use keys to get the values:

dictionaryName['keyName']

we can use this in our case to get the price of a product


#9

You have to write down the prices of the items in food list. In the prices dictionary, the item's name is given as index and the price (real price) as the value. So u have to access the price similar to how we access a value of dictionary. Actually stetim94 is right. To access a dictionary, just use

DictionaryName [index of the value you want].

Also, don't put "return total" in the "for prices in food:" category, if u have.


#10

dictionary's are accessed by key? Not index, that is for lists.

he didn't, he just didn't use markup to make the indent show on the forum


#11

yeah sorry, i just didnt know that it was called key so i named it as index.:innocent:


#12

Got it! finally!

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


#13

I know, and you explanation okay, i just wanted to suggest improvements

@charliehodby good that you solved it :slight_smile:


#14