Can't get the function in exercise 5 right


#1

Okay, so the exercise tells to create the function average. And ithe instructions say:

"0.2 Inside that function, call the built-in sum() function with the numbers list as a parameter. Store the result in a variable called total."

So far okay, i just need to make the following def:

def average(numbers):
total = sum(numbers)

But i got confused with instruction 0.3: "Like the example above, use float() to convert total and store the result in total.". Do i just need to use float(total), and then divide it by len(numbers)?

Here is the current function i'm using:

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

And it returns the error "Oops, try again. average([3, 0]) returned None instead of 1.5 as expected.""

Help please, and thanks in advance.


#2

There are two problems in your code.

As you are expected - you use float() in the wrong way. The problem is that float function returns a new float value, but it does not change the original value.

There are few ways to fix this. You can store result of float in your original variable:

total = float(total)

You can use the same method earlier, pass call to the function sum as a parameter:

total = float(sum(numbers))

If you don't want to modify your original value you can use call to the float as a part of bigger expression:

float(total) / len(numbers)

The second problem is strictly related to the error message. Why your function returned None? Because there are no return statement in your code!

You just have to return result of the final expression, for example (if you use the last method):

return float(total) / len(numbers)

#3

Thanks. I managed to find what i was doing wrong earlier today, but it was more of a "how to i write it" instead of "how to i use float()".

Thanks for the more in-depth explanation.


#4

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


#5

This worked for me:

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