I am trying to write a small bot program which can recognise an image then execute a command if that image exists on the screen. This is heavily PyAutoGUI related. The attatched code below gives a syntax error on the 'if' command:
loadingbattle = pyautogui.locateCenterOnScreen('/Users/devious/loading battle.png');\
if loadingbattle == (2294,1165): pyautogui.click(1513,75)
I am new to programming, so i'm not sure of the problem or if it is the right way to code the idea i've mentioned above. This is just a start to see if the code works by clicking somewhere after it has recognised the image, but the syntax error occurs on the 'if' command and I don't know why. I've watched a few tutorials online but it doesn't seem to solve my problem. Any suggestions as to why this is occuring, or how I can go about coding this idea are appreciated.
The real problem is not the if
statement itself but it's because of the previous statement. Doing if loadingbattle == (2294,1165): pyautogui.click(1513,75)
if perfectly fine in Python, but doing it your way does not work, you will need to change it to this:
loadingbattle = pyautogui.locateCenterOnScreen('/Users/devious/loading battle.png')
if loadingbattle == (2294,1165): pyautogui.click(1513,75)
By getting rid of the ;
and \
, since
;
means that it's the end of the line, it's not necessary to include that at every line break. Equivalent of an "\n
"
\
means that the code will actually continue to the next line (escapes the line break)
So what actually is happening in your code when the Python Interpreter reads it is:
loadingbattle = pyautogui.locateCenterOnScreen('/Users/devious/loading battle.png');if loadingbattle == (2294,1165): pyautogui.click(1513,75)
Which when you try reading it makes absolutely sense. Since you cannot tell what you're trying to assign loadingbattle
to, and where does the if
block ends. That's why indentation matters in python.
Normally, you would like to write it like this to clearly show the indentation (but it actually doesn't matter for your case, both should work fine):
loadingbattle = pyautogui.locateCenterOnScreen('/Users/devious/loading battle.png')
if loadingbattle == (2294,1165):
pyautogui.click(1513,75)