13/13 Let's Check Out! Code Not Printing


This is my code below for the exercise 13/13 Let’s Check Out!
Is there something wrong with my code? I’ve been trying to see what’s the issue myself and looked at previous forum questions, but for some reason my code isn’t printing to the console. It’s just blank.
I’m wasn’t given an error either, I was given a pass on this. I tried closing the tab in case it was just the site being iffy again, but no luck. Any help is appreciated!

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 fruit in food:
    if stock[fruit] > 0:
      total += prices[fruit]
      stock[fruit] -= 1
    return total
  print compute_bill(shopping_list)


the function call (line 25) executes the function, as such, it should be placed outside/after the function. You nested the function call inside the function body


Thank you! A silly mistake… but when I place the function call outside of the body, it gives me back 4. Shouldn’t the answer be 5.5 since only apples are out of stock?


yes, it should return 5.5

by default the final action a function does is to return None, if we want to return something else at the end of the function, we can use the return keyword

So when a return keyword is reached, the function ends

so your function ends at the first iteration of the loop (the return keyword will “break” the loop so the function can end), but to get the correct total, the loop need to make all iterations, how could this be achieved?

seems you have another indent issue


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.