It's okay to be average - code not working


#1



My code gives me error "Oops, try again. average([3, 0]) returned 3.0 instead of 1.5 as expected."
and I have no idea why


It should divide the total with the lenght of numbers.
Here 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)
    total / len(numbers)
    return total

EDIT: Solved it with adding "average = total / len(numbers)" instead of "total / len(numbers)" and returned average


#2

In other cases, to shorten the code a bit use this:

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

#3

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

what is wrong heree please !!?


#4

As what I said before us this:

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

What you're trying to do is the much harder way. Also leading up to doing it wrong. Just use my code! :smiley:

Explanation of how to do this:

1) Define your function: average, with an argument numbers.

2) Make a new variable, total. The value of total will be the float of the sum of numbers.

3) Make another variable called result. The value will be the total divided by the float of the length of numbers.

4) Return Result.

Ta Da!!! There you have it. This nice and simple code.:smile:


#5

this makes sense too. thank you


#6

This work as well

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


#7

My code is not working, I am not sure why, this is what I have so far:

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

EDIT: Solved by putting all total values on one side and using float()


#8

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