Python 2.7定义了一个支持函数来计算平均值

时间:2015-06-26 01:15:56

标签: python python-2.7

我正在尝试定义支持函数getMean(alist)来计算将从另一个函数getStats(city_populations)打印出来的平均值。

我的代码:

def getMean(alist):
    alist = sum(city_population,0.0)/len(city_population)


def getStats(city_populations):
    city_populations.sort(key=int)
    print "The population data in asecending order is:", city_populations
    city_populations.sort(reverse=True)
    print "The population data in descending order is:", city_populations
    print "The max value of the population data is", max(city_populations)
    print "The min value of the populatioon data is", min(city_populations)
    average = getMean(alist)
    print "The mean of the collected data is",average

错误:

  

NameError:未定义名称'alist'

如何启用getStats来使用getMean中的值?

1 个答案:

答案 0 :(得分:2)

# You passed in the wrong variable
average = getMean(city_population)

# The variable you passed is now called 'alist'
# And by returning the average, your function getStats now has access to it
def getmean(alist):
    return sum(alist,0.0)/len(alist)