1

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.

1
  • for num in [numList] does not do what you think it does. [numList] builds a 1-element list whose only element is numList, and then the loop performs 1 iteration with num=numList. Feb 23, 2014 at 4:45

1 Answer 1

5

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())
1
  • Ah I see, the reason it's giving me that error is because I'm trying to compare a function to a number. I tried adding the parentheses to the dictionary where I call it, but I'm still getting the error. Any other suggestion to make the comparison? Or perhaps an entirely different way to attack it? Thanks!
    – plooms
    Feb 23, 2014 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.