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

Question

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

1 Like

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