Return outside function error


#1



https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/1?curriculum_id=4f89dab3d788890003000096#


I thought that my code would run correctly, but it gave the error message 'return outside function. Please help.


Replace this line with your code. 
lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

# Add your function below!
def average(numbers):
    sum(numbers)  
    total = 0
    total += sum(numbers)
    total += float(total)
    total / len(numbers)
return total
<img src="//codecademy-discourse.s3.amazonaws.com/original/4X/3/d/5/3d5e14451660b12661e97aa0357162d002fc44cd.png" width="690" height="431">


#2

I'm not sure what the little html img tag at the bottom of your code is for. But return total should be at the same indentation level as the rest of your code inside the function.


#3

The img was a screenshot of the code, sorry about that.
My code is still giving me an error:Oops, try again. average([3, 0]) returned 6.0 instead of 1.5 as expected.


#4

Keep in mind that += is accumulative addition so total += sum(numbers) will result in total being equal to the sum of numbers. However, when you have total += float(total) it takes the total, which is already set to equal sum(numbers), and it adds total to itself, so total becomes twice what it was initially.

Also the first line of code in your function, "sum(numbers)", is redundant it's not actually doing anything and since you use it later on in total += sum(numers) I would recommend that you remove it.

Finally, make sure with all of your math equations that you are doing something with them I.E. setting a variable equal to the answer of the equation or returning the value, something like that. Don't just have it run and not have the output assigned to anything.


#5

--> The indentation of your Return function is incorrect. Enter a TAB on that line.
--> In python there is no need to initialise a value for newly defined variables, you can directly define
total = sum(numbers)
--> Another thing to notice is you used '+=' function and because of this function the answer is getting wrong. The +=
function will add the value to total.

You can get help from this code below:

Add your function below!

def average(numbers):
total = sum(numbers)
total = float(total)
average = total/len(numbers)
print average
return average

I hope your problem will get solved


#6

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