Average([3, 0]) returned None instead of 1.5 as expected


#1

i keep seeing this error:

average([3, 0]) returned None instead of 1.5 as expected.

This is my 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):
    total = sum(numbers)
    total = float(total)
    average = total / len(numbers)
    print average

#2

That's because you're not returning anything. You're printing the average instead of returning it.


#3

You were right. I have one question though, when can one know when to use the return or print statement?


#4

I'm not completely sure but from what I have seen you use return when you want the computer to see the change and print when you want the computer to show the change to the user. Does that make any sense?


#5

Thanks for your response! Much appreciated


#6

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