Question

I don't know why in my if statements in the for loops it considers them unorderable types to make a comparison with. The exact error I get is as follows:

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

And my code is as follows:

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

I know there is some basic concept I'm missing.

Was it helpful?

Solution

numInput is a function, but you aren't calling it when you pass it to posNumAvg when defining myDict here:

posNumAvg(numInput)

That function is passed to posNumAvg as local variable numList, then is num, then is compared against 0, always referencing the function. Functions and numbers can't be compared, and that's the error you're seeing.

You probably just need to call the function, like this:

posNumAvg(numInput())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top