This forum is now read-only. Please use our new forums! Go to forums
For prob 11/13 Making a Purchase - need some help finding what's wrong
In the following prob - I’m getting an error message the function result is 4, when should be 10.5. It appears I’m either only getting the value for apples, or I’m counting the number of items, rather than summing their costs. Can’t find what’s wrong … any help?
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
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
6 comments
Thanks Jonatan … not sure if thisis the proper way to show the indentation, but its the only thing that worked.
You’re returning too early, you should return after having finished computing the total. As for formatting, highlight it and press the “code sample” button among the formatting tools.. Or get around it by posting it on a pastebin and linking to it. Flex those problem-solving skills.
thanks for the hint … a simple indentation adjustment on the Return did the trick. This is my first day in Code Academy … will get better at it …
Sure sounds like it, you hold yourself responsible which you have to when you’re in the driver’s seat which is what programming is. That doesn’t even have to do with how you relate to others, just about seeing that you’re the one in power.
Some general advice off the top of my head: google everything. Whatever the codecademy banner (error/you passed) says, take it with a huge grain of salt, you’re responsible for checking your code, not it, and it isn’t very good at it either. The errors in the console window can be trusted though, those are straight from Python and are very useful. Debug your code by adding print statements which print out what is happening step by step, you can then compare what’s printed to what you expected. Finding a bug is all about isolating it and then comparing to what it should have looked like (so either what your plan was or if syntax, go google) Name all your variables by what they represent, your code will make so much more sense that way than if you name everything a b c d e f. Enforce your expectations on your code, yet again, you’re the one in power so the code should do as you want, not the other way around. Don’t worry about getting the most efficient implementation of anything, worry about it matching your concept instead, worry about implementing what you had in mind. Sometimes values simply won’t act the way you want, you will have to use something else in those cases, for example, strings cannot be modified, so you would use a list of characters instead, the general concept is still the same and you made the code do what you want. I’m saying the same thing over and over, I know. :P
Oh yes and if there’s a bug that you can’t isolate well enough in your code to figure it out, try to make a simpler scenario of the same thing, adding things to that simpler scenario from your code until the bug or whatever it is, starts occurring.