11. Making a purchase (Total has wrong value)


#1


https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096

I get this error message :
Oops, try again.
compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5


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

It's weird because I don't get what I did wrong.
I created the function and set the value stored inside the variable total to 0 then I set it to increment with the price values of each items.
I have no idea what values have been added to get 4 whereas they clearly shoud add up to 10.5

Thank you

For future reference : as @ionatan showed me : it was the indent that was causing the problem.
In the final return of value of my loop, the indent made the loop terminate and return the value it found right after the first time it goes through the prices list.
The return identation should be at the same level of the first loop statement so it enters in action after it has looped through the whole list, not before. Code should appear like this :slight_smile:

 # Write your code below!
def compute_bill(food):
--total = 0
----for item in food:
------total += prices[item]
----return total

Making a purchase lesson help
#2

Have you tried executing your function manually? Read your code and do what it says in your head, use the input that your error message mentions.

If you arrive at the right result then you've misunderstood something. You can insert prints in your code to make it explain to you what it does as it goes.

Also keep in mind that you can't guess/assume what happens for a line, and you certainly can't assume it'll do the thing you want if you don't know what it does. If you encounter something that you don't know what it will do in that place, then you might just have found the bug and you'll need to look that something up, or perhaps ask about it if it's difficult to create a search query for it.


#3

Absolutely. I tried that method (visualizing what this code does, supposedly, in my head) but I'm still new to the logic behind instructing an interpreter.

Here is what the new code should do :
Define a new function, that takes an argument.
Make a new variable inside this function that has a valuze of zero.
For each item of a variable fed to the function's argument do this :
Take the value of the first item and add it to total : 0 + 4
Take the value of the next item and add it total (total has the value of the previous item) : 4 + 2
Do this for each item you find : 6 + 1.5 + 3
Return that final value.

So I'm missing something that I misunderstood. What would it be ?

Edit : I am also very interested in knowing what steps can I insert print in so that I could so what's going on. But I don't know where to insert additional print statements in my loop.


#4

Something more happens after that, what is the next line in the loop? And what determines where a loop ends?

You can insert prints anywhere in the code, it'll get executed when program control reaches that line. That'll tell you where in the program it currently is and in what order things happened if you have multiple prints. You can also print out variables or the results of expressions. Essentially you would make it print out in plain English what it is doing so that you can simply read it and see what it is doing wrong with no programming knowledge whatsoever.


#5

I got it ! I can't believe I'm not yet fully grasping the indent logic.
I was asking my code to return the value right after the first instance of loop.
So it has been adding the apple value to total : 0 + 4 ---> Then returning that value and halting.

Thanks a lot, dude. It was when you said : Something more happens after that, that I figured out.

You rule [ :


#6

For me , that was the wrong number of indents. I decreased the indents in your code and it was correct. Thank you so much! :smiley:


#7

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