Why is my function triggering multiple times?

This code:

  1. Looks for an image a
  2. If it finds a it tries to find a match for any image in the array image_list
  3. If it finds an image in image_list it looks for e
  4. If it finds e it logs it and moves the mouse to x, y checks for pixel colour and then clicks when when it finds a match, clicks.

This is where my problem arises my avoidLog() function is being called 2-3 times per click. I’ve added print("click") to check if it’s actually clicking and it isn’t, it’s clicking at the right time but for some reason, my log is triggering more than once.

It’s logging correctly, just an odd number of times.

I know python is operating in a single thread in my example so I’m not sure why it’s looping back round and not clicking. It would make more sense to me if it was clicking multiple times and logging multiple times.

import pyautogui as py

def avoidLog():
   avoidLog = open('Avoided.txt', 'a')
   avoidLog.write("Found at: " + str(f) + " at: " + str(skipTime))
   avoidLog.write("n")
   avoidLog.close()

image_list = []
while True:
        if py.locateOnScreen('a.jpg') != None:                       
               for image in image_list:                                          
                    found = py.locateCenterOnScreen(image)

                    if found != None:
                         skipTrigger = py.locateOnScreen('e.jpg')

                         if skipTrigger != None:
                              avoidLog()
                              py.moveTo(x, y)                                                                                           
                              r = py.pixelMatchesColor(x,y, (r,g,b))

                              if r == True:
                                   py.sleep(2)
                                   print("click")
                                   py.click()
                                   break
                                

Answer

avoidLog() is called whenever e.jpg is located. However, py.click() is only called if pixelMatchesColor is True