11. Making a purpose


#1

Please help. It is giving the following error message:
Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'list'

Here is my code:

shopping_list = ["banana", "orange", "apple"]

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

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

#2

here:

total += food

food is a list, how do an integer and a list are suppose to go together?


#3

I don't get what the lesson is asking me to do here.


#4

food (your function parameter) contains a list of products (apple and so on), you need to loop over the list to get the product, then get the price of the product (using the prices dictionary) and add it total

this function computes a bill for the customers purchases


#5

food does not contain those items. The shopping_list contains those items. So should I loop over the shopping_list?


#6

no, you can supply shopping_list as argument when calling the function. The shopping_list then gets copied into the function parameter


#7

I am sorry but I am still very confused on how to do this. Should I place shopping_list under my for? Would doing so bring my list into the equation?
I forgot to include the exercise url so here it is: https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


#8

Lets figure out what this whole code-lesson telling you do !
In a shop there are two things, They have list of fruits they have (with price tag) (price) and also how many each fruit they have (stock).

Now a consumer like us go there to buy things.
But I want to make a program that would calculate the bill for me.
Consumer knows price and stock.

Lets get at code of yours that tries to solve this problem.

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

1.You defined a function that takes list of customer.
(in list consumer have fruits that he needs to buy!)

2.Then to sum up total price of all fruit the customer have to buy,you
created total variable to store the Bill .

3.You looping over your food item
(You're actually checking what fruits consumer want to buy! so each time item takes value of fruit (name of fruit) that consumer will buy!)

  1. You're adding food to total ??
    We can't add list to a number without interpolation(type change).
    You had to add price to total not food? right?
    Can you figure out any list (dictionary) that gives price of a fruit if you
    give fruit as a key? Ya!
    prices['orange']
    gives price of orange!

I hope this hint will work for ya! :slight_smile:


#9

This was great thanks! :slight_smile: If I do what you say it returns 2 times the price of that one item(fruit). For example if I put in total += prices['banana'], it will return 16(twice the price of 1 banana) Here is my updated code that I have spent some time trying to figure out.

shopping_list = ["banana", "orange", "apple"]

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

def compute_bill(food):
    total = 0
    for item in food:
        total += prices['apple']
    return total
n = [4, 2, 1.5, 3]
print compute_bill(n)

#10

problem 1.
n = [4, 2, 1.5, 3]

Explanation:

list here consist of names of fruits that customer have to buy!
for example..

fruits = ['apple' , 'orange']

We pass this list to function , then loop will iterate each of the items, adding price of the fruit to total each time! right?

Update**

Problem 2:
Just noticed the error!
total += prices['apple'] # It will just give price of 'apple'

Let me explain this part---
When we iterate over fruit list of consumer....
say
fruits = ['apple' , 'orange']

Each time our iteration variable >> item takes the value of fruits list.
for item in food:

First item will take value of 'apple'
then second time it will take value of 'orange' !

I hope it helps!! :slight_smile:


#11

I thank you for your help, but it is still not working. Here is what I have done differently:

shopping_list = ["banana", "orange", "apple"]

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

def compute_bill(food):
    total = 0
    for item in food:
        total += prices['banana', 'apple', 'orange', 'pear']
    return total
n = [4, 2, 1.5, 3]
print compute_bill(n)

This is the error message I get in the black window:
Traceback (most recent call last):
File "python", line 23, in
File "python", line 20, in compute_bill
KeyError: ('banana', 'apple', 'orange', 'pear')


#12

Lets start from initial days of loop iteration + Lists .

simple_list = ['a' ,'b','c'] 
for i in simple_list:
    print(i)

It gives output of ...

a
b
c

i is an iteration variable that takes value of list items.

Lets see another code...

dictionary + key

# price of fruits in dictionary
prices = {'apple' : 1, 'orange' : 3, 'pear': 5, 'banana': 6}
print (prices['pear'])

Output will be:

5

List + Dictionary + Key + For loop

# price of fruits in dictionary

prices = {'apple' : 1, 'orange' : 3, 'pear': 5, 'banana': 6}


# a list consist of fruits that I have to buy
fruits = ['banana', 'apple', 'orange', 'pear']
for item in fruits:
    print (item + ' : ' + str(prices[item]))

Output:

banana : 6
apple : 1
orange : 3
pear : 5

What If I have to buy fruits and I made a list like this--

my_fruit_list = ['apple' ,'pear']

so I want to buy apple and pear.
How I will find the total price of fruits?
like this?

total = prices['apple'] + prices['pear']
print (total)

Output will be --

6

But can you do this thing using Loop + Dictionary ?
I'd leave this up to you to figure out!!
I hope it helps and clears your problem a bit more! :slight_smile:


#13

lets start here:

# function with parameter food
def compute_bill(food):
     print food

# function call with argument shopping_list
print compute_bill(shopping_list)

please read the comments in the code, and remember (or read it again if you get stuck on terms i use in my answer) the terms

now, when you call your function with argument shopping_list, the argument gets copied into the function parameter food

so for each item in food, we need to add the price to total

the reason we loop over food, and not shopping_list is because what if a different customer just wants an apple:

# function with parameter food
def compute_bill(food):
     print food

# function call with argument shopping_list
print compute_bill(shopping_list)
# different customer wants to purchase apple
print compute_bill(["apple"])

it would be a bit unfair to charge the second customer for: banana, orange and apple while he only wants to purchases an apple

okay, maybe apple isn't the greatest example, because there are no apples in stock, but you get the idea


#14

I have everything correct to my knowledge but I get the following error: Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2

Here is my code:

shopping_list = ["banana", "orange", "apple"]

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

def compute_bill(food):
    total = 0
    for item in food:
        total += prices['banana'] + prices['apple'] + prices['orange'] + prices['pear']
    return (total)
n = [4, 2, 1.5, 3]
print compute_bill(n)

#15

this is good:

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

now item will contain the product of which you need to get the price and add to total


#16

I am confused. What am I still doing wrong?


#17

n shouldn't contain integers, it should contain products (like shopping_list)

did you read my earlier answer:

your function should work for any list supplied as argument. So you need to add prices[item] to total


#18

I got the same error saying:
Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2

Here is what I changed:

shopping_list = ["banana", "orange", "apple"]

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

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

#19

did you read this? Do you have question about it?

This line:

total += prices['banana'] + prices['apple'] + prices['orange'] + prices['pear']

really needs changing look at this:

def compute_bill(food):
    total = 0
    for item in food:
        total += prices['banana'] + prices['apple'] + prices['orange'] + prices['pear']
    return (total)
n = ['banana','apple','orange','pear']
print compute_bill(n)
print compute_bill(shopping_list)
print compute_bill(['apple'])
print compute_bill(['apple', 'banana'])

your function should work for this all this function calls

this means, all this lists get copied into food function parameter.

Then you loop over food, giving you each item. Then you need to add the price of each item to total


#20

@caydre15
Okay ,I'll explain everything you need to understand in this code.

Lets start again..

We make list like this...

# lets say I have three friends Logan, Wade and Stark
# You can see they're superheroes and participates in war against villains.
# Lets say they went together in 3 fights named them X, Y , Z

# Now each of my friend killed villains in every war(X,Y,Z)

# I have made a dictionary to show this information like this...

Logan = { 'X' : 3,
           'Y' : 6,
           'Z': 12
         }

# What it means? 
# It means Logan have killed 3 villain in X-war, 6 villains in Y-war and 12 villains in Z-war

# Same way defining information about wade and Tony

# wade
Wade  = { 'X' : 30,
          'Y' : 10,
         'Z': 12
         }

# stark
Stark = { 'X' : 1,
          'Y' : 3,
          'Z': 0
         }

# Now I want to find total number of villains  my friends have killed.
# How can I can do it..
#If I loop over my_friends list It will give me name of list

 for name in my_friends:
     print name
# It will print this

# But we want total villains my friends have killed
# If I loop over any dictionary that I created, lets do it...

total = 0 
for war in Logan:
    total += Logan[war]
print total

# It will give you total number of villains Logan have killed. which is 21
# but still it just gives me total no villains killed by one person
````

```
# How to calculate total numbers villain killed by my all friends???

# I put all my friends name in a list , then ?

friend_list = [Logan, Wade, Stark]

# Then we merge three concepts together... Loop + List + Dictionary.

totalVillains = 0
for name in friend_list:
    for war in name:
        totalVillains =+ name[war]

print totalVillains

# It gives you 77 which is total number of villains killed by Logan, Wade and Stark

Do you get the idea?
If yes, then read the last post of @stetim94 and hopefully you will have more clear view on your doubts.
If not then do reply what you didn't get.