5. Problem in average calculating


#1

Hi you all !
When I end with "return moyenne", I do obtain the green pass.
But in fact, I wanted to understand how it works and I ran without.
It gave this

However, the formula is simple and seems to be right...What did I do wrong ?


#2

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

numbers = [1,2,3,4,5,6,7,8,9,0]

average(numbers)

print average (numbers)

"you are not returning anything" that is why you are getting none


#3

Bonjour @bitcoder89423,

The purpose of a function is to return a given value to the computer. The reason why you are receiving that error when you get rid of the return statement is due to the fact that you are not returning a value, hence the 'none'.

I hope that cleared things up for you.

Ciao,

LC


#4

Also, doing:

moyenne = total/len(nombres)

would compromise the definition of the already existing method moyenne. Hence, you should do:

result = total/len(nombres)

i.e., use some other name than moyenne. And then you can do:

return result

#5

Hi you all, and thanks for the help.

Unfortunately, it doesn't really help : I tried diwakar_sharma08's solution but got rid of the "return" because the return gives me the green pass I don't deserve, and I don't learn anything this way...

This is the reason why I replaced it with a "print".
Naturally, it doesn't return anything, but the answer I obtain shows me that the computing is not even done correctly, as you can see below :

What I mean is that I don't really see the "return" statement work; I'd be able to see the result of the computing, shouldn't I ?
How would you do ?

Thanks in advance


#6

dear bitcoder89423,
you have not called the function average(numbers) , how will you get output of print ???


#7

Hi @bitcoder89423,

@diwakar_sharma08 is correct. First of all, you need to call the function. Without calling the function, no statement inside it will ever execute. Just like a can of juice is useless unless you open and drink it.

Secondly, If any function is defined like this:

def testing(lots_of_parameters):
    return 42 # obtained by long computation

and then you want to see if that long computation is correct, so you will do:

print testing(pass_lots_of_arguments)

and the value obtained by long computation would be returned by testing and therefore be printed above.

Hope it helps! :smiley:


#8

Hi you two and thanks for your patience.
Yes it helps, indeed ! I only thought it could be simpler to test...