5/9 Below is my code. it show an error


#1

def average(numbers):
total = 0
def sum(numbers = []):
total += numbers
total = float(total)
total = total / len(numbers)
return float(total)


#2

for the return function it has to be a var not a 'float()' function
also you can put all that inside of 1 line

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

#3

Hi is it possible to have an explanation to the exercise, as i did not understand how you go to that end result?


#4

Hi @beeraider ,

Because your code is not formatted, it is difficult for other users to read and debug it. As code is being posted, you can format it by selecting it, and then by clicking the </> button above the editing area. This will enable us to see important details, such as the indentation and underscores.

You have two function headers in your code, namely, this one ...

def average(numbers):

... and this one ...

def sum(numbers = []):

But without being able to see your indentation, we cannot discern whether the sum function is global or whether it is defined within the average function.


#5

Hi, @ana_catarina,

@ryebread4's solution works by using the sum built in function to provide the total of all the items in the numbers list. To guarantee that this total is of float type, so that when we perform a division, we retain any fractional part that results, the call to sum is enclosed as an argument to a call to the float built in function. Then, that result is divided by the result of calling the len built in function on the numbers list.

To summarize, that solution divides the total of all the items in numbers by the number of items in numbers and returns the result.


#6

makes more sense, thank you very much


#7

Can somebody help me? I don't really get it... :sleepy:


#8

I didn't understand your question, but the outcome of the exercise would be as follows:

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

Careful with indentations!


#10