STUDENT BECOMES THE TEACHER It's Okay to be Average 5/9


#1

really stuck. I believe i have it, but then i’m getting an error on the return. heres the code.
“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):
total=sum(numbers)
total=float(total)
total / len(numbers)
return total

the error is as followed
File “python”, line 25
SyntaxError: ‘return’ outside function

ive tried indenting, etc but it then tells me i have an indentation issue.


#3

Remember that Python uses indentation to define the code blocks. Make sure that all of the code for the function average is indented. Using your code, it would look like this:

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

Problem Lurking

You have a problem in your function too. The first 2 lines are assigning the number into total. Then the 3rd line does the division to get the average, but it’s not assigned or returned. Do you see that?

So when you go to return total, it’s merely a total and not an average.

Think about it. See if you can correct your code.


#4

thank you. I needed to make total/len(numbers) equal to total


#5

And a quick tip for you too. Ready? You can just return like this:

return total / len(numbers)

That gives you one less assignment and line of code. It gets the length, does the division, and then returns the average.

Cheers


#6

The return value will be.

total=float(total)

Namely, the sum in float form.

This line,

is doing nothing.


#7

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