Getting wrong total


#1

Hi @milestonew, @kyleaw
What's the total you're getting? I thought it was supposed to be 7.5 but I keep getting 117, which the system is happy with. It's says congrats you're good to move on, but it doesn't sound right. Thoughts?
Thank you in advance.
San


11. Making a purchase
#2

hi perroud
The total that i got is 10.5
best regards
Barak G


#3

Thanks @baracool, this is what I mean. I the shopping list we have a banana ($4), an orange ($1.5) and an apple ($2). So, from what I understand, it should be 0 (the original value for total) + 4 + 1.5 + 2 = 7.5. And not the 117 that I get or the 10.5 that you did. Unless I'm missing something?!

Below is the function I'm running:

# Write your code below!
def compute_bill(food):
    total = 0
    for n in food:
        total += prices[n]
    return total
print "Total is %s" % (total)

Thoughts?


#4

total does not exist in this scope.

Use either a variable, or a direct insertion of the function call:

print "Total is %s" % compute_bill(shopping_list)

Also, be sure this is not the lesson that explicitly states, "do not print the result".


#5

Thanks for the quick reply @mtf, quick question: if I declare "total" and set it 0 in the beginning of the function, doesn't it make it an existing variable? We even update it later then return its final value. By doesn't exist, do you mean I can't use it outside of the function for being a local variable?


#6

Only in the scope of the function body (the block of code inside the function). The outside world cannot see it. This is how it should be. The less leakage of data we have into the global scope the easier it is to protect our variables and to debug and maintain our code.


#7