Lesson #5: It's OK to be average: float question


#1



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


I thought I had it right, got an error, searched to check for solutions and found a solution where setting the 'total' variable to a 'float' was done on a separate line. Copied, submitted, and passed. Wondered why my solution:
def average(numbers):
total = float(sum(numbers))
total = total / len(numbers)
return total
wasn't working. Went back and did this again. Think I just had a syntax error before. My question is: Is there any reason NOT to set the variable to a float and use 'sum' on the same line? Something that I, as a noob, cannot foresee?


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 = float(sum(numbers))
    total = total / len(numbers)
    return total


#2

@jslattery03

You can simply do the following,

def average(num):
    return float(sum(num)) / len(num)

Any questions?