Why did nothing print to the screen when I pressed Run?

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:

print compute_bill(["banana", "orange", "apple", "banana", "orange", "apple"])
1 Like

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 ?

at the moment, nothing. We need to provide the fruit multiple times as string, or modify the code.

1 Like

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.

2 Likes
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(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)

i was able to pass the page, but still nothing printed on the screen

2 Likes

Did you ever call the function compute_bill()?

the last line of my code is “print compute_bill(shopping_list)” if that’s what you are asking.

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.

capture

shopping_list = [“banana”, “banana”, “orange”, “apple”, “pear”]

print(compute_bill(shopping_list))
print(stock)

returns =>
14.5
{‘orange’: 31, ‘pear’: 14, ‘banana’: 4, ‘apple’: 10} #stock after the purchase.

File “python”, line 25
print compute_bill(shopping list)
^
SyntaxError: invalid syntax

I got this error trying to ‘print total’ and:

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:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1
return total
print compute_bill(shopping list)

You omitted an underscore.

Shopping_list? :cry:

I was so close. Thanks :smiley:

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

1 Like

@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)