[Tutor] a logic problem in an if statement

Steven D'Aprano steve at pearwood.info
Mon Sep 27 00:12:15 CEST 2010


On Mon, 27 Sep 2010 06:01:35 am Bill Allen wrote:

> #This captures the coordinates the two mouse clicks, successfully.
> mouse_pos is in the form (x,y), such as (204,102).
> if mouse_pressed == (1,0,0) and first_click == False:
>         first_click = True
>         mouse_pos1 = mouse_pos
> elif mouse_pressed == (1,0,0) and first_click == True:
>         mouse_pos2 = mouse_pos


You don't actually tell us what mouse_pressed is, I presume (1, 0, 0) 
has some significance. But you check its value twice:

if mouse_pressed = (1, 0, 0) and something-else:
    ...
elif mouse_pressed = (1, 0, 0) and the opposite of something-else:
    ...


That is best written as a single test of mouse_pressed, followed by an 
if-else:

if mouse_pressed = (1, 0, 0):
    if something-else:
        ...
    else:
        ...


What should "something-else" be? Your tests are written as:

first_click == False
first_click == True

But both of them return True or False themselves, so you might as well 
write:

(first_click == True) == True
((first_click == True) == True) == True
(((first_click == True) == True) == True) == True
# Help, help, when do I stop???

Of course nobody would bother with the extra tests, because they're 
redundant. But even doing *one* test is likewise redundant -- the place 
to stop is before you even start! Since first_click is already either 
True or False, all you need is to look at it directly:

if mouse_pressed == (1,0,0):
    if first_click:
        mouse_pos2 = mouse_pos
    else:
        first_click = True
        mouse_pos2 = mouse_pos


I'm not entirely sure about the logic with first_click. It seems to me 
that once it becomes True, it will always stay True, and if it becomes 
False, it will immediately be set to True. But perhaps you have some 
other code elsewhere that handles setting it to False.



> #This is the decisional logic based on the mouse click coordinates,
> previously captured.
> #This first case fails although the print statement give me feedback
> on the coordinates,
> #that suggest it should have worked.  It always falls through to the
> final else.
> if mouse_pos[1] < height/2 and mouse_pos2[1] > height/2:
[...]

I'm going to suggest another approach here. Since you only care whether 
the clicks are in the top or bottom half of the screen, let's make that 
explicit:

click_in_bottom_half1 = mouse_pos[1] < height/2
click_in_bottom_half2 = mouse_pos2[1] < height/2

Now your decision logic becomes simple, and obvious. It documents 
itself:

if click_in_bottom_half1 and click_in_bottom_half2:
    print "Both clicks in bottom half of screen"
elif click_in_bottom_half1:
    print "The first click was in the bottom half."
    print "The second click was in the top half."
elif click_in_bottom_half2:
    print "The first click was in the top half."
    print "The second click was in the bottom half."
else:
    print "Both clicks in top half of screen"



-- 
Steven D'Aprano


More information about the Tutor mailing list