[Tutor] a logic problem in an if statement

Bill Allen wallenpb at gmail.com
Sun Sep 26 22:01:35 CEST 2010


Ok, I am have a problem with some logic in a piece of code I am working
on.   I have tinkered with it for hours and am stumped.  Pretty sure I have
lost sight of the forest for the trees...

The purpose of this code is to take the coordinates on screen of the mouse
at the time of two mouse clicks, store that information and then make some
decisions based on that.  No matter what I do, I am finding that the second
if statement block falls through to the last option.   In the if I am try to
say "if the mouse click cursor position with the first click was in the
upper half of the screen (i.e. < height/2) and the second mouse click was in
the lower half of the screen (i.e. > height/2)".  elif "both clicks happened
in the upper half of the screen"   else "assume the clicks occurred in the
lower half of the screen".  The second case and last case always seem to
work out ok.  The first case falls through to the final case even though the
coordinates were appropriate for the first case logic.

Any thoughts how I am going wrong here?

--Bill

#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

#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:
    print("use 0 for new yadjust")
    print(height/2,"did first one", mouse_pos1[1], mouse_pos2[1])

#This case always works.
elif mouse_pos[1] < height/2 and mouse_pos2[1] < height/2:
    print("use {0} for new
yadjust".format(1.05+(1-((height/2)-mouse_pos1[1])/(height/2))))
    print(height/2,"did 2nd one", mouse_pos1[1], mouse_pos2[1])

#this case always works.
else:
    print("use {0} for new
yadjust".format(-1.07+(1+((height/2)-mouse_pos2[1])/(height/2))))
    print(height/2,"did last one", mouse_pos1[1], mouse_pos2[1])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100926/cfe64e1f/attachment.html>


More information about the Tutor mailing list