-1

Im using pyautogui and detecting multiple images through three if statements. But only the top if statement is activated, even though the other if statements should be true according to previous tests

Here is the code:

from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con

while 1:
    try:
        if (pyautogui.locateOnScreen(r"C:\Users\Daniel Fernando\Desktop\Python\chromeDinosaur\assets\one.png", region=(755,325,120,120), grayscale = True, confidence=0.6) is not None):
            print("i see one")
            keyboard.send(" ")
            
        elif (pyautogui.locateOnScreen(r"C:\Users\Daniel Fernando\Desktop\Python\chromeDinosaur\assets\three.png", region=(740,325,120,120), grayscale = True, confidence=0.5) is not None):
            print("i see three")
            keyboard.send(" ")
              
        elif (pyautogui.locateOnScreen(r"C:\Users\Daniel Fernando\Desktop\Python\chromeDinosaur\assets\two.png", region=(740,325,120,120), grayscale = True, confidence=0.5) is not None):
            print("i see two")
            keyboard.send(" ")


    except pyautogui.ImageNotFoundException:
        print()
5
  • 2
    Are you sure you know how elif (else if) works? If first statement is always true, other ones will never be executed. Commented May 5 at 9:00
  • Yes, refer to my comment on the answer below for my explanation Commented May 5 at 9:47
  • you write "other if statements" but you don't have other if but elif - and this makes difference. You should use if in all places. Commented May 5 at 16:45
  • in python you can use while True instead of while 1 Commented May 5 at 16:47
  • using empty print() in except can be wrong idea - you may have some error but you don't know it - you could display it except pyautogui.ImageNotFoundException as es: print(ex) Commented May 5 at 16:48

1 Answer 1

0

Problem 1:

Your two following elif-conditions will never even be tested, if the first condition is already True. elif in python is a contraction for else if, so it will only be checked if the previous condition is False.

Replace your elif by if to fix your code.

Refer to this reference for a detailed description.

Simple example:

a = 2

if a > 0:  # This condition is checked first
    print("a is positive")
elif a % 2 == 0:  # Will never be reached if a > 0
    print("a is even")  

Problem 2:

pyautogui.locateOnScreen() throws an empty pyautogui.ImageNotFoundException when it does not detect an image. You have to have separate try/except-blocks around the detection to avoid being thrown to the end of the loop upon the first mismatch.

Sign up to request clarification or add additional context in comments.

5 Comments

I also thought of that situation, but I checked, and even though the first statement is false, and the following statement are true, it still does not execute. I also experimented with only using if instead of elif, but it still occurs.
OK, please edit your code in the exception block to except pyautogui.ImageNotFoundException as e: and print(e) and post your console output. 2nd best guess it the first condition is already throwing, which is why you never get to evaluate the other two in that case.
I tried it and it just printed blank space
I separated each if statement to different try catch statements and it worked. It was likely to have been what you said that the first condition was already throwing.
I also checked that a second ago. Bad thing is that the exception that pyautogui.locateOnScreen() is throwing is empty. This is why it prints an empty line. Hard to debug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.