Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

353 views
Skip to first unread message

George Molsom

unread,
May 10, 2016, 5:02:00 AM5/10/16
to
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?



import time

def second(int):
time.strftime("%S")

start = input('Press enter when you are ready to start')
time1 = time.strftime("%S")

then = time.time()

end = input('Press enter when you think 10 seconds has passed')
time2 = time.strftime("%S")

def totaltime(int):
(time2-time1)

if totaltime == '10':
print ('YOU ACTUALLY DID IT')

if totaltime < 10:
print ('Haha you took too long! Your result was:', totaltime,'seconds')

if totaltime > 10:
print('Too early TRY AGAIN! Your result was:', totaltime, 'seconds')





Press enter when you are ready to start
Press enter when you think 10 seconds has passed
Traceback (most recent call last):
File "E:/Computing/Python/Little book of challenges/Challenge 6 10 seconds experiment.py", line 20, in <module>
if totaltime < 10:
TypeError: unorderable types: function() < int()

Ben Finney

unread,
May 10, 2016, 5:17:25 AM5/10/16
to
George Molsom <georgie...@gmail.com> writes:

> I have created a program in class 'make a game that tests how good
> people are at guessing when 10 seconds has elapsed.'

Welcome! You may want to join the dedicated beginners forum
<URL:https://mail.python.org/mailman/listinfo/tutor> where we
collaboratively teach foundational Python concepts.

> Traceback (most recent call last):
> File "E:/Computing/Python/Little book of challenges/Challenge 6 10 seconds experiment.py", line 20, in <module>
> if totaltime < 10:
> TypeError: unorderable types: function() < int()

That's right. The ‘totaltime’ name refers to a function. To ask whether
an integer object is less than a function object is not a meaningful
comparison.

If you want to *call* the function, use the “call this function”
syntax::

totaltime()

That will evaluate to the return value when you call it, so use the
return value::

if totaltime() < 10:
# …

--
\ “There's a certain part of the contented majority who love |
`\ anybody who is worth a billion dollars.” —John Kenneth |
_o__) Galbraith, 1992-05-23 |
Ben Finney

Chris Angelico

unread,
May 10, 2016, 8:31:24 AM5/10/16
to
On Tue, May 10, 2016 at 10:19 PM, Dennis Lee Bieber
<wlf...@ix.netcom.com> wrote:
> You invoke strftime() [in default: use current time mode], but pass it
> a format that is not defined in the documentation (or wasn't in Python 2.7
> which I'm still running). #2
> %s (lowercase) formats the seconds field of the time of day. And worst,
> since you don't return the result, it just gets thrown away. #3
>

Sorry to nitpick, but this isn't the case. time.strftime("%S") is what
you're talking about (getting the "seconds since beginning of current
minute"). There is a "%s" format string, but it's not supported on all
platforms; it actually would be more useful here, as it represents the
time as seconds since 1970 (aka "Unix time").

Of course, time.time() would still be far more useful here, as you
subsequently recommended.

ChrisA

Steven D'Aprano

unread,
May 10, 2016, 9:40:41 AM5/10/16
to
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

Chris Angelico

unread,
May 10, 2016, 9:48:10 AM5/10/16
to
On Tue, May 10, 2016 at 11:40 PM, Steven D'Aprano <st...@pearwood.info> wrote:
> 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.
>

But the times were being calculated at one-second resolution, which
solves that but raises its own issues.

ChrisA

Steven D'Aprano

unread,
May 10, 2016, 9:55:38 AM5/10/16
to
Ah, I didn't realise that time.strftime("%S") was one-second resolution
(obvious in hindsight, duh!) and the call to time.time() is apparently
unused.



--
Steven

0 new messages