首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >实现字典函数来计算列表的平均值

实现字典函数来计算列表的平均值
EN

Stack Overflow用户
提问于 2015-06-22 20:37:52
回答 2查看 1.3K关注 0票数 2

和往常一样,我已经尝试了一段时间,然后在这里提出一个问题。我知道有好几次尝试来回答这个问题,但没有一次真正有效地满足了我的需要。

以下是说明:

实现以下三个函数(应该使用适当的循环构造来计算平均值):

代码语言:javascript
运行
复制
allNumAvg(numList) : takes a list of numbers and returns the average of all the numbers in the list.
posNumAvg(numList) : takes a list of numbers and returns the average of all the numbers in the list that are greater than zero.
nonPosAvg(numList) : takes a list of numbers and returns the average of all the numbers in the list that are less than or equal to zero.

编写一个程序,要求用户输入一些数字(正数、负数和零)。您的程序不应该要求用户输入固定数量的数字。此外,它不应该要求用户输入的数字数量。相反,它应该要求用户输入几个数字并以-9999 (一个哨兵值)结尾。用户可以按任何顺序输入数字。您的程序不应该要求用户分别输入正数和负数。

然后,您的程序应该创建一个包含输入的数字的列表(确保不包括这个列表中的哨兵值(-9999) ),并输出具有以下键值对的列表和字典(使用输入列表和上面的函数):

代码语言:javascript
运行
复制
Key = 'AvgPositive'  :  Value = the average of all the positive numbers
Key = 'AvgNonPos'  :  Value = the average of all the non-positive numbers
Key = 'AvgAllNum'  :  Value = the average of all the numbers

样本运行:

输入一个数字(-9999结束):4

输入一个数字(-9999结束):-3

输入一个数字(-9999结束):-15

输入一个数字(-9999结束):0

输入一个数字(-9999结束):10

输入一个数字(-9999结束):22

输入一个数字(-9999结束):-9999

输入的所有数字清单如下:

4,-3,-15,0,10,22

有平均数的字典是:

{“AvgPositive”:12.0,“AvgNonPos”:-6.0,“AvgAllNum”:3.0}

这是我的密码:

代码语言:javascript
运行
复制
a = []
b = []
c = []
dictionary = {}
total = 0

print("Enter positive, negative or zero to determine the average: ")
while(True):
    user_input = int(input("Enter a number (-9999 to end): "))
    if(user_input == -9999):
        break

def allNumAvg(values):
    for number in a:
        total = total + number
        average = sum(total) / len(total)
        if user_input > 0 or user_input < 0:
            a.append(user_input)
    return average


def posNumAvg(values):
    for number in b:
        total = total + number
        average = sum(total) / len(total)
        if user_input > 0:
            b.append(user_input)
    return average

def nonPosAvg(values):
    for number in c:
        total = total + number
        average = sum(total) + len(total)
        if user_input < 0:
            c.append(user_input)
    return average

print("The list of all numbers entered is:")
print(a+b+c)

dictionary = {
    "AvgPositive": posNumAvg(values),
    "AvgNonPos": nonPosAvg(values),
    "AvgAllNum": allNumAvg(values)
}



print("The dictionary with the averages are:")
print(dictionary) 

我的问题是如何实现从字典中打印的平均值,因为我目前正在得到:"AvgPositive": posNumAvg(values), NameError: name 'values' is not defined的错误。此外,如何将输入的数字列表打印出来?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-22 20:50:11

我想你想要的更像是:

代码语言:javascript
运行
复制
print("Enter positive, negative or zero to determine the average: ")
# get list of values/numbers from the user
values = [i for i in iter(lambda: int(input("Enter a number (-9999 to end): ")), -9999)]

-9999是中断循环的前哨值。

代码语言:javascript
运行
复制
def allNumAvg(values):
    # get average of all numbers
    return sum(values) / len(values)


def posNumAvg(values):
    # get only positive numbers
    values = [v for v in values if v > 0]
    return sum(values) / len(values)

def nonPosAvg(values):
    # get all negative numbers
    values = [v for v in values if v < 0]
    return sum(values) / len(values)

print("The list of all numbers entered is:")

# pass list of values to each function
dictionary = {
    "AvgPositive": posNumAvg(values),
    "AvgNonPos": nonPosAvg(values),
    "AvgAllNum": allNumAvg(values)
}

如果要在循环中创建三个列表,请检查在正确列表后面的for循环中的每个i:

代码语言:javascript
运行
复制
a = [] # all
p = [] # pos
n = [] # neg

print("Enter positive, negative or zero to determine the average: ")
values = []
for i in iter(lambda: int(input("Enter a number (-9999 to end): ")), -9999):
    if i >= 0: # if positive append to p
        p.append(i)
    else: # else must be negative so append to n
        n.append(i)
    a.append(i) # always add to a to get all nums


def allNumAvg(values):
    return sum(values) / len(values)

def posNumAvg(values):
    return sum(values) /len(values)

def nonPosAvg(values):
    return sum(values) / len(values)

print("The list of all numbers entered is:")

# pass correct list to each function
dictionary = {
    "AvgPositive": posNumAvg(n),
    "AvgNonPos": nonPosAvg(p),
    "AvgAllNum": allNumAvg(a)
}
票数 1
EN

Stack Overflow用户

发布于 2015-06-22 20:44:55

也许是这样的.(如果它不能正常工作,就可以调试,因为它是作业!)你需要定义“价值观”

代码语言:javascript
运行
复制
values = []

print("Enter positive, negative or zero to determine the average: ")
while(True):
    user_input = int(input("Enter a number (-9999 to end): "))
    if(user_input == -9999):
        break
    values.append(user_input)

这应该能让你开始。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30989386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档