Python 类型错误:无序类型:函数()>;int()
我不知道为什么在我的Python 类型错误:无序类型:函数()>;int(),python,python-3.x,pydev,Python,Python 3.x,Pydev,我不知道为什么在我的if语句中,在for循环的for循环中,它会将它们视为无序类型来进行比较。我得到的确切错误如下: Traceback (most recent call last): File "/Users/me/Documents/Eclipse/Week6/src/numbers.py", line 44, in <module> myDict = {'AvgPositive':posNumAvg(numInput), 'AvgNonPos':nonPosAvg
if语句中,在for循环的for循环中,它会将它们视为无序类型来进行比较。我得到的确切错误如下:
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()
回溯(最近一次呼叫最后一次):
文件“/Users/me/Documents/Eclipse/Week6/src/numbers.py”,第44行,在
myDict={'AvgPositive':posNumAvg(numInput),'AvgNonPos':nonPosAvg(numInput),'AvgAllNum':allNumAvg(numInput)}
posNumAvg中的文件“/Users/me/Documents/Eclipse/Week6/src/numbers.py”,第30行
如果num>0:
TypeError:无序类型: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)
#用于用户输入的While循环函数
def numInput():
数字=[]
尽管如此:
num=int(输入('输入一个数字(-9999结尾):'))
如果num==-9999:
打破
numbers.append(num)
返回号码
#所有数的平均值函数
def allNumAvg(numList):
返回金额(numList)/len(numList)
#所有正数函数的平均值
def posNumAvg(numList):
对于[numList]中的num:
如果num>0:
posNum=总和(num)
posLen=len(num)
返回posNum/posLen
#所有负数函数的平均值
def非OSAVG(numList):
对于[numList]中的num:
如果num<0:
negNum=总和(num)
negLen=len(num)
返回negNum/negLen
#创建字典
myDict={'AvgPositive':posNumAvg(numInput),'AvgNonPos':nonPosAvg(numInput),'AvgAllNum':allNumAvg(numInput)}
#打印列表
打印('输入的所有数字的列表为\n',numInput(),'\n')
#印刷词典
print('平均值为\n',myDict的字典)
我知道我缺少一些基本概念 numInput
是一个函数,但是当您在此处定义myDict
时,将其传递给posNumAvg
时,您不会调用它:
posNumAvg(numInput)
该函数作为局部变量numList
传递给posNumAvg
,然后是num
,然后与0
进行比较,始终引用该函数。函数和数字无法比较,这就是您看到的错误
您可能只需要调用函数,如下所示:
posNumAvg(numInput())
numInput
是一个函数,但是当您在此处定义myDict
时,将它传递给posNumAvg
时,您不会调用它:
posNumAvg(numInput)
该函数作为局部变量numList
传递给posNumAvg
,然后是num
,然后与0
进行比较,始终引用该函数。函数和数字无法比较,这就是您看到的错误
您可能只需要调用函数,如下所示:
posNumAvg(numInput())
对于[numList]中的num,
并不像您想象的那样[numList]
构建一个单元素列表,该列表的唯一元素是numList
,然后循环对[numList]
中的num执行一次迭代num=numList
[numList]
构建一个单元素列表,其唯一元素是numList
,然后循环使用num=numList
执行1次迭代。啊,我明白了,它之所以给我这个错误是因为我试图将函数与数字进行比较。我试着在我称之为字典的地方加上括号,但还是有错误。还有其他的比较建议吗?或者是一种完全不同的攻击方式?谢谢啊,我明白了,它给我这个错误的原因是因为我试图将一个函数与一个数字进行比较。我试着在我称之为字典的地方加上括号,但还是有错误。还有其他的比较建议吗?或者是一种完全不同的攻击方式?谢谢