Why did nothing print to the screen when I pressed Run?
Answer
To see your results be sure to call your compute_bill() function in a print statement and pass it the shopping_list list variable as an argument. Like this: print compute_bill(shopping_list).
How do we compute for the different number of fruits that we want the compute_ bill function to perform. Currently it is returning the answer 5.5 for the parameter (shopping _list) as it is considering only a single amount for each fruit. When I try to convert (shopping_list) to a dictionary and add different values for the keys(fruits), it returns the same value 5.5 ?
shopping_list represents a list of products the customer wants to buy. The customer canāt determine the price, the prices are fetched from prices dictionary. So if prices of products need to change, prices needs to be changed
we can pass different lists to compute_bill, which represents customers. for example:
Thank you for the reply. Besides the method that you have given above, what would be the other methods, if we were to calculate the total bill for the various quantities of each fruit that the customer would buy ?
what you could also do, would be adding a third dictionary saying the quantity you want and then setting the total to be price * quantity, but in this case you are modifying the code.
Well, looking at the lesson, page 12 wants only the function, not the calling statement; your function passes with the calling statement commented out. Then, page 13 ( the final page) does ask for the calling statement, which does print to the screen, as expected.
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)
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.