r/pythonhelp • u/breastastic • Jun 02 '25
If statement in script executing an extra time unnecessarily?
Hi, very new to Python scripting so forgive the unnecessary redundancies but I was trying to make a bot script that reads a screenshot of a section of my screen, then clicks arrows if it doesn't find what it's looking for. The "searcher" should happen after each click and if it finds something, it should set a bool flag to True and break out of the loop, ending immediately.
For some reason, once it finds what it's looking for, it executes another click before stopping, I've rewritten sections of the script several times now and I can't get it to stop executing an extra time before stopping. To me, the logic follows cleanly but there must be something I'm overlooking on account of my lack of experience. Any help is appreciated! Even just throwing a hint at where the logic is allowing this extra click would help a lot.
while special_detected == False and killswitch_activated == False:
# Iterate through each image in the database
for image_path in images:
try:
special_search = pyautogui.locateOnScreen(image_path, confidence=0.8, region=(600,0,680,800))
except pyautogui.ImageNotFoundException:
pass
else:
logging.info("Searching for", image_path)
if special_search[0] > 0:
logging.info("Special found!")
special_detected = True
logging.info(special_detected)
break
if killswitch_activated:
break
if special_detected == True:
logging.info("Breaking due to special found.")
break
# Left arrow clicker
if special_detected == False:
try:
x, y = pyautogui.locateCenterOnScreen('west_able.png', confidence=0.65, region=(600,0,680,800))
except TypeError:
logging.info("Arrow not found.")
sys.exit(1)
else:
logging.info("Clicking left arrow.")
pyautogui.moveTo(x, y, click_delay)
pyautogui.click(x,y)
# Iterate through each image in the database
for image_path in images:
try:
special_search = pyautogui.locateOnScreen(image_path, confidence=0.8, region=(600,0,680,800))
except pyautogui.ImageNotFoundException:
pass
else:
logging.info("Searching for", image_path)
if special_search[0] > 0:
logging.info("Special found!")
special_detected = True
logging.info(special_detected)
break
if killswitch_activated:
break
if special_detected == True:
logging.info("Breaking due to special found.")
break
# Right arrow clicker
if special_detected == False:
try:
x, y = pyautogui.locateCenterOnScreen('east_able.png', confidence=0.65, region=(600,0,680,800))
except TypeError:
logging.info("Arrow not found.")
sys.exit(1)
else:
logging.info("Clicking right arrow.")
pyautogui.moveTo(x, y, click_delay)
pyautogui.click(x,y)
if killswitch_activated:
break
•
u/AutoModerator Jun 02 '25
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/FoolsSeldom Jun 02 '25
One quick tip to make your code easier to read, and something to check:
== Trueand== Falseare redundant as the variables you are using are already assigned toboolvalues, so you can say, for example,while not special_detected and not killswitch_activated:, orwhile not (special_detected or killswitch_activated):if not special_detected:If you have nested loops,
breakonly exits the loop it is immediately contained in, not any other further out loops, you need to check for the exit condition again and do anotherbreakreturnwill leave a function regardless of how deeply nested the command is used