Making a Purchase


#1

So i already completed it but I'm wondering why I'm not computing the total for the shopping list we just made. I thought that was the whole point of this. Also to top it the sum doesn't even add up. i just want to know what the code should actually be. I've constructed an unfinished code that ill post but I want to learn it properly.


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

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

# Write your code below!
def sum(prices):
    total = 0 
    for number in prices:
        total += (prices[number])
    return total 
compute_bill = shopping_list
print sum(compute_bill)


#2

this code passes? I find that difficult to believe.

lets start here:

def sum(prices):

sum() is a built in function, are you sure you want to overwrite it? use compute_bill like the exercise suggest
prices (your function parameter) is also the name of your dictionary, i recommend you use food like suggested by the exercise. the in your for loop loop over food.

here:

total += (prices[number])

don't use parentheses, parentheses create a set (a immutable list)


#3

no i said its unfinished code but im trying figure out what it would be if we were actually trying to figure out the shopping list. like i said before i already passed it with the code they want you to make


#4

if you did it right, the bill should be computed? Can i see the code you used to pass the exercise?


#5

can you post the full code?


#6

i already asked for the code so i can see what is wrong, what do you need it for that you have to ask for it again?


#7

4 posts were split to a new topic: Making a purchase


#11

2 posts were split to a new topic: Making a purchase


#12

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

print(compute_bill(shopping_list))

Here instead of food it takes arguments from the list "shopping_list " for the item in shopping_list it takes the key value prices from the dictionary . .
Hope you get what i said


#14

People, please read the guidelines, if you have a question make a new topic, this topic is for helping e-kard.

If you want to help (help, not provide the code solution) feel free to reply, but please otherwise don't, consult the guidelines on what you should do


#15

why do you need the code when i already said I passed it. That's not the point here I'm wondering why we weren't computing for the shopping list as I've mentioned before, but obviously people cant read what was previously wrote. What i'm asking is a for a simple side project/proper code thing. But if it so important to see my passed code here it is.

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 s in food:
        total += (prices[s])
    return total

#16

Thank you this is what i was looking for. So this is how it should actually look then correct?


Not really getting this compute_bill thing
#17

i misinterpreted your question, let met set it straight. When we add a function call, we need to supply an argument to satisfy the function parameter:

# function declaration
def example(s):
    return s
 
# function call
print example("hello world")

not a very interesting example, but it shows the point. You can do the same, call compute_bill, supply shopping_list as argument and voila.

If you add a print statement before the function call, print will print what the function return's

note: function only execute when called


#18

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