SyntaxError: 'return' outside function"

Asked by Janet

I want to realize the function that if find de picture "img", then return 0.
But when i excute this script,it occures the error as bellow:

if find(img):
    return 0

[sikuli] Error message:
SyntaxError: ("'return' outside function", ('C:\\DOCUME~1\\ADMINI~1\\LOCALS~1\\Temp\\sikuli-tmp298073160123893407.py', 2, 1, ''))

Has anyone seen this error before and been able to solve it? I can't seem to find anything that leads to a solution.
That would be very gratfully for your help!

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Janet
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

return is only allowed in a function (python: def anyname():)

use exit() instead, that stops executing your script.

To use the result of a find operation for an if, I recommend to us exists() instead, which does not stop the script when the find fails. It returns none instead and the script continues.

So in your case:

if exists(img):
    exit()

Revision history for this message
Janet (wangyupep) said :
#2

Thank you very much, now i am sure what is this problem!