You omitted an underscore.
Shopping_list?
I was so close. Thanks
Ok so stupid question but how does the function know to fetch the shopping_list list? and index it against the prices and stock? You define a function to compute the bill and run a for loop on items in “food” but how does Python know that “food” is the shopping list and compare it to the dictionaries?
at line 25, you call the function, where you provide an argument (shopping_list
) for the parameter (food
)
the nice thing about this, is that we can call the function multiple times with different shopping lists, representing different customers buying different things
Okay, that makes sense, I think I was just looking at it to linearly (again like I said, stupid question). Also, when the print function isn’t there it doesn’t throw and error, the code isn’t wrong its just not being called.
Thank you
That list is suppied as the argument to the compute_bill()
function. As to the dictionaries, they are hard-coded into the function.
Mine wont connect. Or print anything and i did what they said to do.
Please see this topic:
How to ask good questions (and get good answers)
Won’t connect to what? The codecademy workspace? Could you make a screenshot of this? Along with the other things mentioned in the topic
@tstone6 print statement is being called in the for statment. remove the indention before you call print. like this:
def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1
return total
print compute_bill(shopping_list)
i followed the same code but still could not print
Difficult to say what the problem is without actual seeing the code? Maybe also a screenshot so we can see what you see?
If I have done a return total ---- why does print total not work?
There is very little information for us to work with in the question; therefore, we draw assumptions.
- is the print statement inside the code block, but after
return
? - is the print statement outside of the function, but
total
a variable of the function?
Sorry, as this question was linked from a specific lesson, I had some assumptions about people knowing the context:
As suspected earlier… print
after return
.
Remember that immediately after a return
statement is executed, the function is exited. This means that any code written after a return
statement will not be executed.
Thank you so much!! I finally got it to print to the console lol. It was that pesky stock**[item]** -= 1
i got the same thing. but figured out that there is empty space infront of the print. if you take that out. it will print to the screen
Check your indentation:
def compute_bill(shopping_list):
total = 0
for item in shopping_list:
if stock[item]>0:
total += prices[item]
stock[item] -= 1
return total
print compute_bill(shopping_list)
The print command should not be indented:
def compute_bill(shopping_list):
total = 0
for item in shopping_list:
if stock[item]>0:
total += prices[item]
stock[item] -= 1
return total
print compute_bill(shopping_list)