TypeError:unorderable类型:function()> INT()

时间:2014-02-23 04:42:02

标签: python python-3.x pydev

我不知道为什么if statements for loops中的Traceback (most recent call last): File "/Users/me/Documents/Eclipse/Week6/src/numbers.py", line 44, in <module> myDict = {'AvgPositive':posNumAvg(numInput), 'AvgNonPos':nonPosAvg(numInput), 'AvgAllNum':allNumAvg(numInput)} File "/Users/me/Documents/Eclipse/Week6/src/numbers.py", line 30, in posNumAvg if num > 0: TypeError: unorderable types: function() > int() 认为它们是无法比较的类型。我得到的确切错误如下:

#While loop function for user input
def numInput():
    numbers = []

    while True:
        num = int(input('Enter a number (-9999 to end):'))
        if num == -9999:
            break
        numbers.append(num)
    return numbers

#Average of all numbers function
def allNumAvg(numList):
    return sum(numList) / len(numList)


#Average of all positive numbers function
def posNumAvg(numList):
    for num in [numList]:
        if num > 0:
            posNum = sum(num)
            posLen = len(num)
    return posNum / posLen

#Avg of all negative numbers function
def nonPosAvg(numList):
    for num in [numList]:
        if num < 0:
            negNum = sum(num)
            negLen = len(num)
    return negNum / negLen

#Creates Dictionary
myDict = {'AvgPositive':posNumAvg(numInput), 'AvgNonPos':nonPosAvg(numInput), 'AvgAllNum':allNumAvg(numInput)}   

#Prints List
print ('The list of of all numbers entered is\n', numInput(),'\n')

#Prints Dictionary
print ('The dictionary with averages is\n', myDict)

我的代码如下:

{{1}}

我知道我缺少一些基本概念。

1 个答案:

答案 0 :(得分:4)

numInput是一个函数,但在此处定义posNumAvg时将其传递给myDict时,您不会调用它:

posNumAvg(numInput)

该函数作为局部变量posNumAvg传递给numList,然后是num,然后与0进行比较,始终引用该函数。无法比较函数和数字,这就是您所看到的错误。

您可能只需要调用该函数,如下所示:

posNumAvg(numInput())
相关问题