TypeError: unorderable types: function() < int()

Steven D'Aprano steve at pearwood.info
Tue May 10 09:40:25 EDT 2016


Hello George, and welcome!


On Tue, 10 May 2016 07:01 pm, George Molsom wrote:

> I have created a program in class 'make a game that tests how good people
> are at guessing when 10 seconds has elapsed.'
> 
> The following are the code I currently have and the error produced when I
> attempt to run it. I have tried everything I can think of to resolve the
> issue, and I have also run the code through a checker, which has said that
> there are no mistakes. I have also shown a friend who is a programmer, and
> he cannot find a problem with it. The teacher doesn't actually know the
> solution to the problem so I was wondering if someone could point me in
> the right direction to get this working please?

Neither your friend who is a programmer nor the teacher can read an error
message?

> TypeError: unorderable types: function() < int()

In fairness, it is a bit of a rubbish error message. But what is it saying
it that you are trying to check whether a function is less than a number.
You are not comparing the *result* of calling the function with the number,
but the function itself.

For example, suppose we have a function that (to keep it simple) always
returns 3.

def func():
    return 3

Now you go to check whether it is less than some other number:

if func() < 9:
    print("smaller")


That's what you *meant* to do, but unfortunately you left off the round
brackets (parentheses), which means that instead of calling the function
and then comparing the result of that with 9, you compare the FUNCTION
itself with 9. 

if func < 9: ...

Obviously this is nonsense. You can't compare functions with integers, the
question is ludicrous. So Python rightly complains with an error message.

The immediate fix is to add the brackets in so that you call the function
first to get a result, and then compare the result with the number.


Another comment about your game:

You are asking the player to hit enter at 10 seconds. *Exactly* ten seconds.
The computer can measure time to well under a millionth of a second, I'm
pretty sure that nobody, no matter how skillful, can be expected to hit
enter after exactly 10.000000 seconds, not 10.000001 or 9.999999 seconds.

I think it is reasonable to give them a little bit of slack to be above or
under 10 seconds by a fraction of a second. So you should round the time to
one decimal place:

totaltime = round(totaltime, 1)

before checking whether it is equal to 10.



-- 
Steven




More information about the Python-list mailing list