[Tutor] A range of numbers

R. Alan Monroe amonroe at columbus.rr.com
Mon Dec 1 19:16:38 EST 2003


>> I was wondering if it is possible to set a variable or something to a
>> RANGE of numbers. For instance , I want to have it so that when I
>> mouse clicks in a certain region( let say, from (100,100) to
>> (200,200)), it prints out "You Clicked!" . I'm doing this with
>> Livewires right now. Of course, it would take forever if i try to write:
>>
>> if mouse_position() == (100,100) or (100,101) or (100,102)
>> ..............................
>> and mouse_buttons()['left'] == 1:
>> do something


>>>> y = 4
>>>> y in range(5)
> True # 1 in older Versions of Python
>>>> y in range(5,10)
> False # or 0

> so if you first extract the coordinates of
> your mouse-position:
>>>> x,y = mouse_position()
> then you can easily check if y is in the desired
> range.

I haven't tried it, but that seems like it would be slow, because
Python would have to create a list, possibly hundreds of numbers long,
in RAM, every time you checked it. Unless Python caches them?

The traditional way I think is to use greater than and less than.

if (x>100) and (x<200) and (y>100) and (y<200):
   print "you clicked"

Alan




More information about the Tutor mailing list