Skip to main content
 首页 » 编程设计

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

2024年10月24日19artech

一如既往,在我继续在这里提问之前,我已经尝试了一段时间。我知道有几次尝试来回答这个问题,但没有一个真正能满足我的需要。

以下是说明:

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

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))并输出列表和具有以下键值对的字典(使用输入列表以及上述功能):

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}

这是我的代码:

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。此外,我如何获得要打印的输入数字列表?

谢谢!

请您参考如下方法:

我想你想要的更像是:

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 是打破循环的标记值

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:

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) 
}