我正在尝试定义支持函数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
中的值?
答案 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)