[Tutor] Me again

Vsevolod Sipakov python-tutor at vs.megalink.ru
Fri Aug 13 14:34:31 CEST 2004


Hello,

On Wed, Aug 11, 2004 at 06:54:33PM -0400, jason hochstein wrote:
> This is what I get when using the %.
> 
> l_input=[]
> 
> while True:
>     s = raw_input("Type number. Hit enter for more or type quit to finish: ")
>    
>     if s == "quit":
>         break
>     else:
>          l_input.append(float(s))
> 
> for i in l_input:
>         average = sum(l_input) / len(l_input)
>         middle = len(l_input) % 2 

don't use the loop, average should be computed ONCE.

def average(l):
    return sum(l) / len(l)

and median can be computed like this:

def median(l):
    half = len(l) // 2
    if (len(l)%2)==1:
        return l[half]
    else:
        return sum(l[half-1:half+1])/2.0

print "The mean of this list is", average(l_input)
print "The median of this list is", median(l_input)

-- 
Vsevolod Sipakov


More information about the Tutor mailing list