Making a Purchase

#1

Oops, try again. compute_bill(['apple']) resulted in a NameError: global name 'total' is not defined

Here is my code https://gist.github.com/7b15a322de6eeaae6407c9bdd757b7e0
I've looked at a few posts where people were stuck. I know my code is REALLY wrong but i would appreciate an explanation on why. For example, i know that `for item in shopping_list:` is incorrect. It should read something like `for item in food:`
What i don't understand is why? How does that programme know what `food` is and also what it contains?
I've also seen that people have set total to zero like this `total = 0`. Why is this correct? Is `total == 0` incorrect?

``````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 shopping_list:
item += prices[key]
total == item + total

#2

two equal signs means check if equal, is that really what you want to do here?

a function parameter (`food`) is a placeholder, until you call the function and supply an argument (for example shopping_list) at function call

a function ends the moment a return keyword is reached, thus, if a return keyword is reached in a loop, the loop will break

so your function calculates the cost for a single item, what if a customer wants to buy multiply items?

#3

Problem1.
As mod mentioned

Problem2.
indent your return as same level as of for loop
`return total`

#4

No i want to set total to zero

Sorry im still a long way off getting my head round thinking like a coder

#5

but to assign a value to a variable, you should use a single equal sign.

#6

Here is what i was thinking when creating the code please tell me what I should be thinking:

``````def compute_bill(food):
#setting the total to zero
total = 0
#for loop that looks for things inside of food ( not sure how )
for item in food:
# adding the item and the price in price list
item += prices[key]
#changing the number in total to be the total number + the value held in item.
total == item + total
#exiting the for loop with return

#7

look:

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

# function call with shopping_list argument
compute_bill(shopping_list)``````

see how the arguments get copied into the function parameter?

here:

``item += prices[key]``

item should be the key to get price of item, which you can add to total directly

#8

Devil(bug) is there--

``````       # adding the item and the price in price list
item += prices[key]
#changing the number in total to be the total number + the value held in item.
total == item + total``````
1. item is a iteration variable,don't use it to store something ,you already made a variable total to store prices..
2. key is item !
3. `total == item + total` ,we're not comparing things out ( == ),
delete one of the lines from them and assign price to total at each looping/iteration!

#9

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