Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108457 > unrolled thread

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

Started byGeorge Molsom <georgieelize00@gmail.com>
First post2016-05-10 02:01 -0700
Last post2016-05-10 19:57 -0400
Articles 8 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  TypeError: unorderable types: function() < int() George Molsom <georgieelize00@gmail.com> - 2016-05-10 02:01 -0700
    Re: TypeError: unorderable types: function() < int() Ben Finney <ben+python@benfinney.id.au> - 2016-05-10 19:16 +1000
    Re: TypeError: unorderable types: function() < int() Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-05-10 08:19 -0400
    Re: TypeError: unorderable types: function() < int() Chris Angelico <rosuav@gmail.com> - 2016-05-10 22:31 +1000
    Re: TypeError: unorderable types: function() < int() Steven D'Aprano <steve@pearwood.info> - 2016-05-10 23:40 +1000
      Re: TypeError: unorderable types: function() < int() Chris Angelico <rosuav@gmail.com> - 2016-05-10 23:47 +1000
        Re: TypeError: unorderable types: function() < int() Steven D'Aprano <steve@pearwood.info> - 2016-05-10 23:55 +1000
    Re: TypeError: unorderable types: function() < int() Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-05-10 19:57 -0400

#108457 — TypeError: unorderable types: function() < int()

FromGeorge Molsom <georgieelize00@gmail.com>
Date2016-05-10 02:01 -0700
SubjectTypeError: unorderable types: function() < int()
Message-ID<1fc32599-0264-460c-8178-057558d19be5@googlegroups.com>
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()

[toc] | [next] | [standalone]


#108458

FromBen Finney <ben+python@benfinney.id.au>
Date2016-05-10 19:16 +1000
Message-ID<mailman.558.1462871831.32212.python-list@python.org>
In reply to#108457
George Molsom <georgieelize00@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

[toc] | [prev] | [next] | [standalone]


#108461

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2016-05-10 08:19 -0400
Message-ID<mailman.562.1462882760.32212.python-list@python.org>
In reply to#108457
On Tue, 10 May 2016 02:01:45 -0700 (PDT), George Molsom
<georgieelize00@gmail.com> declaimed the following:

>
>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?
>

	Really -- neither the instructor nor a programmer can find any problems
in the code sample? I count ten problems minimum, just scanning the code.

	A checker won't find anything since every statement is valid Python
syntax -- it just doesn't do what you expect.

>
>
>import time
>
>def second(int):
>    time.strftime("%S")
>    
	You declare "second" as a function taking one argument, but you never
use the argument (hint: (int) is not a declaration of a return type).	#1

	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

	Good thing you never call your second() function.	#I won't count that

>start = input('Press enter when you are ready to start')
>time1 = time.strftime("%S")
>
	This time you save the string form of the seconds of the time-of-day

>then = time.time()
>
	And here you actually get numerical clock value. Of course, a few
milliseconds may have passed while the previous statement was processed, so
this time will be different from the time used in strftime().

>end = input('Press enter when you think 10 seconds has passed')
>time2 = time.strftime("%S")
>
	Same comment as under "start"

>def totaltime(int):
>    (time2-time1)
>
	Again you define a function with an input argument which is not used.
#4

	time2 and time1 are STRINGS; you can not subtract them from each other.
#5
They are also only the second field from a time-of-day which means it is
possible that they crossed over a minute boundary:

	TOD1	11:59:59		time1	59
	TOD2	12:00:09		time2	9

	time2 - time1		-50			#6

	Again you do not return the value computed in totaltime, so it is not
available for other uses later.		#7

>if totaltime == '10':

	As explained by others, you aren't calling totaltime, you are only
referencing the function object itself.		#8

	You are also expecting it to return a character string rather than a
number, since it is a character string you are comparing against.

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

	This time you are comparing against an integer value...	Which is it
supposed to be		#9 (either the character is wrong, or the integer is
wrong)

>    print ('Haha you took too long! Your result was:', totaltime,'seconds')
>
	If the "player" took LESS THAN 10 seconds you print that they too too
long.		#10

>if totaltime > 10:
>    print('Too early TRY AGAIN! Your result was:', totaltime, 'seconds')
>
	And if they took more than 10 seconds you say they were too early. #not
counted

	You also attempt to call totaltime 5 times in that section of code,
even though the result won't change.		#not counted


-=-=-=-=-
#   Python 2.7 syntax used

import time

raw_input("Press [enter] to start timing")  #throw out input
strt = time.time()

raw_input("Press [enter] when you believe 10 seconds have passed")
end = time.time()

delta = end - strt

#   strt, end, and delta are floats, per the documentation
#   so round it to the nearest second, and make it integer
seconds = int(round(delta))

if seconds < 10:
    print "You were too fast! Your time was %s seconds" % seconds
elif seconds > 10:
    print "You were too slow! Your time was %s seconds" % seconds
else:
    print "You were on 10 seconds (give or take half a second)"
-=-=-=-=-
{Not really a "reaction timer" but only name I could come up with at the
moment}

C:\Users\Wulfraed\Documents\Python Progs>reactionTimer.py
Press [enter] to start timing
Press [enter] when you believe 10 seconds have passed
You were too slow! Your time was 12 seconds

C:\Users\Wulfraed\Documents\Python Progs>reactionTimer.py
Press [enter] to start timing
Press [enter] when you believe 10 seconds have passed
You were too fast! Your time was 8 seconds

C:\Users\Wulfraed\Documents\Python Progs>
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#108462

FromChris Angelico <rosuav@gmail.com>
Date2016-05-10 22:31 +1000
Message-ID<mailman.563.1462883467.32212.python-list@python.org>
In reply to#108457
On Tue, May 10, 2016 at 10:19 PM, Dennis Lee Bieber
<wlfraed@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

[toc] | [prev] | [next] | [standalone]


#108463

FromSteven D'Aprano <steve@pearwood.info>
Date2016-05-10 23:40 +1000
Message-ID<5731e4ca$0$1603$c3e8da3$5496439d@news.astraweb.com>
In reply to#108457
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

[toc] | [prev] | [next] | [standalone]


#108464

FromChris Angelico <rosuav@gmail.com>
Date2016-05-10 23:47 +1000
Message-ID<mailman.564.1462888074.32212.python-list@python.org>
In reply to#108463
On Tue, May 10, 2016 at 11:40 PM, Steven D'Aprano <steve@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

[toc] | [prev] | [next] | [standalone]


#108466

FromSteven D'Aprano <steve@pearwood.info>
Date2016-05-10 23:55 +1000
Message-ID<5731e84d$0$1615$c3e8da3$5496439d@news.astraweb.com>
In reply to#108464
On Tue, 10 May 2016 11:47 pm, Chris Angelico wrote:

> On Tue, May 10, 2016 at 11:40 PM, Steven D'Aprano <steve@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.

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

[toc] | [prev] | [next] | [standalone]


#108504

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2016-05-10 19:57 -0400
Message-ID<mailman.582.1462924638.32212.python-list@python.org>
In reply to#108457
On Tue, 10 May 2016 22:31:00 +1000, Chris Angelico <rosuav@gmail.com>
declaimed the following:

>On Tue, May 10, 2016 at 10:19 PM, Dennis Lee Bieber
><wlfraed@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").
>
	Interesting -- I had the help page open at the time looking for a cap-S
format...

	Maybe not sorted order?

	OKAY, I was wrong on that one -- I'll blame the use of an old
prescription for my glasses in the morning... THIS time I can make out that
it IS a capital S. (The display font in the help viewer may have
contributed -- the % appears taller than the S)
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web