In [1]:
def mean_calc(input_list):
    list_len = 0 # variable to track running length
    list_sum = 0 # variable to track running sum
    if input_list:
        for i in input_list:
            if isinstance(i,int) or isinstance(i,float): # check to see if element i is of type int or float
                list_len += 1
                list_sum += i
            else: # element i is not int or float
                print "list element %s is not of type int or float" % i
        return list_sum/float(list_len) #return the final calculation
    else: #list is empty
        return "input list is empty"

In [2]:
test_list = [1,1,1,2,3,4,4,4,4,5,6,7,9]

In [3]:
mean_calc(test_list)


Out[3]:
3.923076923076923

In [4]:
import numpy as np

In [5]:
np.mean(test_list)


Out[5]:
3.9230769230769229

In [6]:
test_string = ['1',2,'3','4']

In [7]:
mean_calc(test_string)


list element 1 is not of type int or float
list element 3 is not of type int or float
list element 4 is not of type int or float
Out[7]:
2.0

In [8]:
np.mean(test_string)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-1b70377e298d> in <module>()
----> 1 np.mean(test_string)

/Users/richarddunks/anaconda/lib/python2.7/site-packages/numpy/core/fromnumeric.pyc in mean(a, axis, dtype, out, keepdims)
   2733 
   2734     return _methods._mean(a, axis=axis, dtype=dtype,
-> 2735                             out=out, keepdims=keepdims)
   2736 
   2737 def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):

/Users/richarddunks/anaconda/lib/python2.7/site-packages/numpy/core/_methods.pyc in _mean(a, axis, dtype, out, keepdims)
     64         dtype = mu.dtype('f8')
     65 
---> 66     ret = umr_sum(arr, axis, dtype, out, keepdims)
     67     if isinstance(ret, mu.ndarray):
     68         ret = um.true_divide(

TypeError: cannot perform reduce with flexible type

In [9]:
empty_list =[]

In [10]:
mean_calc(empty_list)


Out[10]:
'input list is empty'

In [11]:
np.mean(empty_list)


/Users/richarddunks/anaconda/lib/python2.7/site-packages/numpy/core/_methods.py:59: RuntimeWarning: Mean of empty slice.
  warnings.warn("Mean of empty slice.", RuntimeWarning)
/Users/richarddunks/anaconda/lib/python2.7/site-packages/numpy/core/_methods.py:71: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
Out[11]:
nan

In [ ]: