多个条件问题 if 语句 Python
Issue with Multiple condition if statement Python
我正在编写一个程序来移动我的鼠标(使用 pyautogui lib),如果它在 x 秒内没有移动的话。我在开始时两次获取 X、Y 坐标,然后在时间延迟后再次获取 X、Y 坐标,然后将 X 和 Y 值与前一个值进行比较。我的 if 语句有问题,理论上应该执行上述操作,但在测试后它没有按预期工作。任何人都可以建议我可以进行任何编辑来解决这个简单的问题。
这是我的代码:
#!/usr/bin/env python3
import pyautogui
import time
currentMouseX, currentMouseY = pyautogui.position() #Grabs X,Y mouse position
print("position X1 is", currentMouseX)
print("position Y1 is", currentMouseY)
X1 = currentMouseX
Y1 = currentMouseY
time.sleep(3)
currentMouseX2, currentMouseY2 = pyautogui.position() #Grabs second X,Y position after 3 seconds
X2 = currentMouseX
Y2 = currentMouseY
print("position X2 is", currentMouseX2)
print("position Y2 is", currentMouseY2)
**if ((X1 == X2) and (Y1 == Y2)):
print ("!!! MOVE MOUSE !!!")
else:
print("Mouse does not need to be moved")**
仅供参考:我已将 if 语句保留得非常简单,因为我希望它在继续该程序之前能够正常工作。非常感谢任何帮助。
与其测试相等性,不如测试差异是否低于某个阈值可能更好:
moveThresh = 4 # (or suitable small number)
XMove = X2 - X1
YMove = Y2 - Y1
if abs(XMove) < moveThresh and abs(YMove) < moveThresh:
# treat tiny moves as no move
print("The mouse is effectively stationary & the cat is bored")
else:
print("The mouse is moving & the cat is interested")
等等
除非您要连接一些有趣的硬件,否则我怀疑您会移动鼠标 - 只会移动鼠标指针。
注意:说你的代码没有按预期工作是没有意义的,除非你解释它应该做什么以及它实际在做什么。
话虽如此,看你的代码我假设你的问题是你总是得到结果“!!! MOVE MOUSE !!!”,即使你确实移动了你的鼠标。
如果仔细查看代码,您会发现 X1 和 X2 始终相同,Y1 和 Y2 也始终相同,因为您使用以下方式分配它们:
X1 = currentMouseX
Y1 = currentMouseY
和
X2 = currentMouseX
Y2 = currentMouseY
不覆盖 currentMouseY
。相反,您将第二个坐标加载到 currentMouseX2
和 currentMouseY2
.
简而言之,您的代码使用了很多不必要的赋值方式。相反,请执行以下操作:
#!/usr/bin/env python3
import pyautogui
import time
prev = pyautogui.position() #Grabs X,Y mouse position
print("position X1 is", prev[0])
print("position Y1 is", prev[1])
time.sleep(3)
after = pyautogui.position() #Grabs second X,Y position after 3 seconds
print("position X2 is", after[0])
print("position Y2 is", after[1])
if (prev == after):
print ("!!! MOVE MOUSE !!!")
else:
print("Mouse does not need to be moved")
我正在编写一个程序来移动我的鼠标(使用 pyautogui lib),如果它在 x 秒内没有移动的话。我在开始时两次获取 X、Y 坐标,然后在时间延迟后再次获取 X、Y 坐标,然后将 X 和 Y 值与前一个值进行比较。我的 if 语句有问题,理论上应该执行上述操作,但在测试后它没有按预期工作。任何人都可以建议我可以进行任何编辑来解决这个简单的问题。
这是我的代码:
#!/usr/bin/env python3
import pyautogui
import time
currentMouseX, currentMouseY = pyautogui.position() #Grabs X,Y mouse position
print("position X1 is", currentMouseX)
print("position Y1 is", currentMouseY)
X1 = currentMouseX
Y1 = currentMouseY
time.sleep(3)
currentMouseX2, currentMouseY2 = pyautogui.position() #Grabs second X,Y position after 3 seconds
X2 = currentMouseX
Y2 = currentMouseY
print("position X2 is", currentMouseX2)
print("position Y2 is", currentMouseY2)
**if ((X1 == X2) and (Y1 == Y2)):
print ("!!! MOVE MOUSE !!!")
else:
print("Mouse does not need to be moved")**
仅供参考:我已将 if 语句保留得非常简单,因为我希望它在继续该程序之前能够正常工作。非常感谢任何帮助。
与其测试相等性,不如测试差异是否低于某个阈值可能更好:
moveThresh = 4 # (or suitable small number)
XMove = X2 - X1
YMove = Y2 - Y1
if abs(XMove) < moveThresh and abs(YMove) < moveThresh:
# treat tiny moves as no move
print("The mouse is effectively stationary & the cat is bored")
else:
print("The mouse is moving & the cat is interested")
等等
除非您要连接一些有趣的硬件,否则我怀疑您会移动鼠标 - 只会移动鼠标指针。
注意:说你的代码没有按预期工作是没有意义的,除非你解释它应该做什么以及它实际在做什么。
话虽如此,看你的代码我假设你的问题是你总是得到结果“!!! MOVE MOUSE !!!”,即使你确实移动了你的鼠标。
如果仔细查看代码,您会发现 X1 和 X2 始终相同,Y1 和 Y2 也始终相同,因为您使用以下方式分配它们:
X1 = currentMouseX
Y1 = currentMouseY
和
X2 = currentMouseX
Y2 = currentMouseY
不覆盖 currentMouseY
。相反,您将第二个坐标加载到 currentMouseX2
和 currentMouseY2
.
简而言之,您的代码使用了很多不必要的赋值方式。相反,请执行以下操作:
#!/usr/bin/env python3
import pyautogui
import time
prev = pyautogui.position() #Grabs X,Y mouse position
print("position X1 is", prev[0])
print("position Y1 is", prev[1])
time.sleep(3)
after = pyautogui.position() #Grabs second X,Y position after 3 seconds
print("position X2 is", after[0])
print("position Y2 is", after[1])
if (prev == after):
print ("!!! MOVE MOUSE !!!")
else:
print("Mouse does not need to be moved")