EvilZone

Programming and Scripting => Beginner's Corner => : IrishYoga September 01, 2015, 08:24:55 PM

: Creating a function that finds the average of a list in python
: IrishYoga September 01, 2015, 08:24:55 PM
Hello, I have this code that I think should be giving me the average of a list but when I put it into python, the error says error:division by zero. I'd appreciate it if you guys looked at this code and told me what i'm doing wrong.
: (python)
def f(x):
    d = []
    if x == d:
        return None
    else:
        sum = 0
        for n in d:
            sum = sum + n
        length = len(d)
    total = sum/length
    print(total)
   
: Re: Creating a function that finds the average of a list in python
: flowjob September 01, 2015, 08:37:44 PM
: (python)
def f(x):
    d = []
    if x == d:
        return None
    else:
        sum = 0
        for n in d:
            sum = sum + n
        length = len(d)
    total = sum/length
    print(total)

Because "d" is an empty array, so length is zero. And you're dividing by "d"s length in line 10.

Also, this code can be improved quite a bit:
Line 2 is unneccessary
Line 3 could be changed to "if x:", as an array x evaluates to false if it is empty and to true if it isn't. Then you could put the rest of the code inside that "if".
Line 8 can be shortened to "sum += n".
Line 11 should be "return total", if you want to use the average for some other stuff.
Also, improve variable naming, to be more clear what a variable represents.
Also, "sum" is a built-in function, so don't use it as a variable name!

This would be the improved version:

: (python)
def calcAverage(_array):
    if _array:
        _sum = 0
        for i in _array:
            _sum += i
        average = _sum/len(_array)
        return average
    else:
        return None
: Re: Creating a function that finds the average of a list in python
: IrishYoga September 01, 2015, 09:30:57 PM
Thanks for not only putting the correct code but also pointing out what I did wrong! This really helped.
: Re: Creating a function that finds the average of a list in python
: flowjob September 01, 2015, 09:42:06 PM
Also, I forgot to mention that if you enclose your code with the [ c o d e ] and [ / c o d e ]  tags it gets formatted to be easier to read. You can also specify a language for syntax-highlighting like this (e.g. python): [ c o d e = p y t h o n ]
: Re: Creating a function that finds the average of a list in python
: novaccainne October 06, 2015, 03:32:05 PM
Just for fun :

:
if  type(myarray) is list and  len(myarray) > 0 :
sum(myarray)/len(myarray) == sum([x for x in myarray]) / len(myarray) == reduce(lambda x,y: x+y,myarray) / len(myarray)


http://caisbalderas.com/blog/iterating-with-python-lambdas/
http://www.python-course.eu/lambda.php
: Re: Creating a function that finds the average of a list in python
: fayesafe October 16, 2015, 01:35:31 PM
In Python 2, you could also use the builtin reduce()-function. (Consider functools.reduce() for Python 3.) It then looks like that:

: (python)
l = [1, 2, 3, 4, 5, 6, 7, 8]
average = reduce(lambda x, y: x + y, l) / float(len(l))