5. It's Okay to Be Average


#1



5. It's Okay to be Average


Error Message: average([3, 0]) resulted in an error: global name 'total' is not defined


Not sure what this error message means. My code is below. Thank you!


Replace this line with your code. 

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


#2

Are you sure that's the code you ran? I don't think it produces that error message.


#4

replace:

result = total / len(numbers)

return result

with:

total = total / len(numbers)

return total


#5

I couldn't tell you why introducing the result variable would not work. Maybe because CodeCademy's console is expecting a certain manner of coding and an only single new variable (the total). I was suspecting that and @noidea1 confirmed it.

Other than that your code is correct.

For the sake of being concise, though. I wrote the instructions like this :

 total = (float(sum(numbers)))/len(numbers)
    return total

It's just your three lines of codes boiled down to a single line using the magic of parentheses. :wink:


#6

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