实现字典功能来计算列表的平均值

时间:2015-06-22 20:37:53

标签: python list dictionary

与往常一样,在我开始在这里提问之前,我已经尝试了一段时间。我知道有几次尝试回答这个问题,但没有一个尝试过我需要的东西。

以下是说明:

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

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(哨兵值)结束。用户可以按任何顺序输入数字。您的程序不应要求用户单独输入正数和负数。

然后你的程序应该创建一个包含输入数字的列表(确保不在此列表中包含sentinel值(-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结束): - 999

输入的所有数字列表为:

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

谢谢!

2 个答案:

答案 0 :(得分:1)

也许是这样的......(你可以调试它是不是按原样工作,因为它的功课!)。您需要定义&#39;值&#39;

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 :(得分:1)

我想你想要更像的东西:

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