A day at the supermarket, making a purchase


Hello everyone, this is my first post here, I've been trying to learn how to program. For the most part i haven't had a whole lot of trouble, however I currently have a situation where the code isn't doing what i expect it to do, can someone review my code and tell me where i'm off, I don't want a completely different code then I wouldn't learn.

the error i'm getting is "apple" returned 1.5 instead of 2.. so for some reason in the calculations it's sorting the list numerically, but still using the keys alphabetically. Is there some way to keep my coding the same and maybe add a .sort function or something??


It would be immensely helpful for us if we have your raw code to run and test. Please post it in a new reply, and I'll format it this one time, after which you can learn how to post code samples (look for the three backticks thread). Will watch for your reply.



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):
    for key in prices:
        return total


Should read,

total += prices[key]


Thank you for the reply, I changed it to that, however i'm still getting the error 'apple returned 1.5 instead of 2'


After formatting the code sample we see the indentation on the return line needs to be backed off one level.


I see now, that fixed the issue, it does what I originally expected it to do now, it adds all the totals together. Not sure why but now it's saying that compute_bill'apple' returned 10.5 instead of 2.. I don't understand the task I guess.. I thought it was to add all the totals together, which is what we did. Thank you for helping me understand the problem I was having though!


I think I found my issue, the instructions say to have it compute from the 'food' list.. I just figured it would save coding time to make the food list the same as the price list for this task, it does the same thing, does it not?


Actually, no, it does not. The shopping_list is a list, stock and prices are dictionaries. When we pass food to the compute_bill() function, we are passing a list. That list inside the function is shopping_list but with a local name.

When we iterated over prices in the loop, it may have overlapped the list, but it did not correspond with it, like it should have. It added all the prices, only.

for item in food:
    total += prices[item]
return total

Now the items in the shopping_list are the corresponding key in the prices dictionary. Hopefully this is what you have discovered. It may seem cruel, but I was saving that for last, in case you didn't spot it.


I appreciate the help! I think maybe I should go back a few lessons and drill things in better, I don't quite understand some things.


Yes, it is a good idea to stick with things until you gain some understanding and confidence. There is no race and no hurry. Take as much time as you need and do some off site research and reading to help clarify each new concept and topic. Programming is not easy, so everything we can do to give ourselves a leg up brings with it some ease as we continue learning. Happy coding!


actually, I am in EXACTLY the same situation. I have gone over and over the previous lessons, and still do not get it!