0

I think i have the function for mean but i also need to figure out below mean. This what i have so far.

import math

def mean(alist):
    belowMean = alist
    
    mean = sum(alist) / len(alist)
    if mean <= alist:
        belowMean = item

    return belowMean
5
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by Schol-R-LEA
0

item is undefined. If type of mean is float, then alist > mean as 'list' > 'float'

Edited by pyTony: n/a

0

Hold on here. Getting the mean (or average) is a fairly simple thing in and of itself:

import math

def mean(*alist):
    return sum(alist) / float(len(alist))

What you're doing doesn't seem to be aimed at just getting the mean of a list of values, however. What is item for, and just what should this function of yours return?

Edited by Schol-R-LEA: n/a

0

after i find the mean , i need to find the numbers that is under mean

0

Add some swear words. That will make it mean. Also, add some print statements so you can see what the program is doing. And if you can not complete the problem, try it another way. Perhaps computing the mean by adding each element of the list to a total field, via a for loop. Obviously you do not understand what this code is doing so staring at it will not help; a different method may.

def mean(alist):
    belowMean = alist
 
    mean = sum(alist) / len(alist)
    print "I am mean, GRRRRR", mean
    print "comparing for <=", mean, "with", alist
    if mean <= alist:
        belowMean = item
 
    return belowMean

Edited by woooee: n/a

0

This may be what you want, to return a list of items below the mean.

def mean(alist):
    belowMean = []
    mean = sum(alist) / len(alist)
    for item in alist:
        if item<mean:
            belowMean.append(item)
 
    return belowMean
0

I keep getting meanBelow not defined

import math
def mean(alist):
    
    meanBelow =[]
    mean = sum(alist) / len(alist)
    for item in  alist:
        if item < mean :
            meanBelow.append(item)   
    return   meanBelow
0

Odd... the code as given works fine on my system, in both the 2.6 and 3.0 interpreters. What version of Python are you using?

You might want to try using list() instead of [] when initializing meanBelow, as that's a little more explicit, but they both should work the same way in any version of the language I know of.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.