Not sure how my code is wrong but it is


I am getting the error:
compute_bill([‘banana’, ‘apple’, ‘orange’, ‘pear’]) returned 4 instead of 10.5

def compute_bill(food):
  total = 0
  for key in food:
    total += price[key]
    return total


By default, a function returns None at the end. If at the end of the function we want to return something different, we can use the return keyword. So, using the return keyword will end the function (even when return keyword is reached in the loop), so your function ends in the first iteration of the loop but to get to the correct total, the loop should make all its iterations, so return shouldn’t be inside the loop


A post was split to a new topic: Python problem


Hi @mikmazey,

You should have a variable named prices that is assigned a dictionary by this statement …

prices = {"banana": 4,"apple": 2,"orange": 1.5,"pear": 3}

However, in your loop, you refer to a variable named price, as follows …

    total += price[key]


My task is
Write a for-loop that iterates over start_list and .append()s each number squared (x ** 2) to square_list.Then sort square_list!

start_list = [5, 3, 1, 2, 4]
square_list = []

Your code here!

for number in start_list:
square_list =start_list.append(number**2)

print type(square_list)
print square_list


print square_list


you are appending to start_list here:


please explain to me, why you would do this?


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