0

I'm making a bot using pyautogui, at a certain point I need the function locateOnScreen to loop until it finds the desire image, searching throught internet I found this way of looping my code:

t = pyautogui.locateOnScreen("Templates.png")
while t == None:
    t = pyautogui.locateOnScreen("Templates.png")
    print("Templates found!")
pyautogui.click(t, clicks=2)

This method works perfectly fine, but my editor keeps warning me that a comparison to None should be if cond is None:

Example:

t = pyautogui.locateOnScreen("Templates.png")
if t is None:
    t = pyautogui.locateOnScreen("Templates.png")
    print("Templates found!")
pyautogui.click(t, clicks=2)

But when I use this method the loop doesn't work anymore. I want to know the differences between these two methods and If the first method I'm using is the right way to do it.

Joy
  • 33
  • 3
  • 1
    That's not because of `is` vs `==`. Why did you remove the loop? `if` is not a loop! – user2357112 Nov 10 '21 at 06:09
  • 2
    Isn't it just suggesting that you write `while t is None:`, instead of `==`? (i.e., it's not about `if`, but about `==`.) – j1-lee Nov 10 '21 at 06:09
  • (If your editor specifically told you to use `if`, that's an issue you should report on your editor's bug tracker.) – user2357112 Nov 10 '21 at 06:10
  • You are using `while` in the first code block while `if` in the second code block. That's the reason it shows different behavior. However, it's preferable to use `is None` instead of `== None` in Python. – Nouman Ahsan Nov 10 '21 at 06:11
  • I agree that the [wording of the flake8 rule E711](https://www.flake8rules.com/rules/E711.html) is misleading but you _shouldn't_ change the `while` to `if`. – Selcuk Nov 10 '21 at 06:12
  • I have submitted a pull request, let's see if it will get merged. – Selcuk Nov 10 '21 at 06:19

0 Answers0