exists doesen't seem to work inside def!!

Asked by GZ

When I am trying to do this, I get an error message for some reason and I can't seem to figure it out why:

def foo():
 if exists(someimage.jpg, 1):
  stuff='Hello'
 elif exists(someotherimage.jpg, 1):
  stuff='World'
foo()

print stuff

[error] NameError ( name 'stuff' is not defined )

BUT it works like this:

if exists(someimage.jpg, 1):
 stuff='Hello'
elif exists(someotherimage.jpg, 1):
 stuff='World'
print stuff

I have also tried this, but no luck, still not working:

def foo()
 img1 = exists(someimage.jpg, 1)
 img2 = exists(someotherimage.jpg, 1)

 if img1 != None:
  stuff='Hello'
 elig img2 != None:
  stuff='World'
foo()

print stuff

I really don't know whats wrong..
Any help or advice is much appreciated!!

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
GZ
Solved:
Last query:
Last reply:

This question was reopened

  • by GZ
Revision history for this message
Eugene S (shragovich) said :
#1

Hi,

This is not a Sikuli but rather a Python question.
You cannot directly access a variable defined inside the function. All the variables you initiate inside the function are in scope of that function only and are invisible outside the function.

So if you want to make a variable available outside function you generally have 2 options:

1. Return the variable. THIS IS THE RELEVANT IN YOUR CASE.
You should make the function return the variable, which is a standard procedure.
To do that, just add a line:

"return stuff"

at the end of your function.
Then you will be able to call you function in this manner:

print foo()

or:

result = foo()
print result

2. Make the variable global (This is just for general knowledge. Not the way to do that in this case)
You can define a variable as global in your function like that:

stuff = ''

def foo():
    global stuff
    stuff = 'hello'

foo()

print stuff

Hope that helps.

Regards,
Eugene

Revision history for this message
GZ (g-zr) said :
#2

Hi Eugene,

Thanks a lot!! I've learnt a lot from this! :)

Both solutions are really cool!

Thank You again for the prompt reply.

Best Regards,
George

Revision history for this message
GZ (g-zr) said :
#3

Thanks Eugene S, that solved my question.

Revision history for this message
GZ (g-zr) said :
#4

Hi Eugene,

For some reason this is not always working, it sometimes returns me icon=a even though it is not even on the screen.

def whichIconVisible():
 icon=""
 if exists(imageOfIcon1, 0) or exists(otherImageOfIcon1, 0):
  icon="a"
 elif exists(imageOfIcon2, 0) or exists(otherImageOfIcon2, 0):
  icon="b"
 elif exists(imageOfIcon3, 0) and variable=="this":
  icon="c"
 return icon

while blah > 10:
 icon = whichIconVisible()
 print icon
 doubleClick(something)
 .
 .
 .
 .
 .

I really don't know what I am doing wrong, please help me.

Best Regards,
George

Revision history for this message
Eugene S (shragovich) said :
#5

Hi,

Each time you're not sure why something was or wasn't detected on the screen, use highlight() function. Read about it here:

http://doc.sikuli.org/region.html#Region.highlight

When a pattern is detected, you will see its parameters. Maybe there is a similarity threshold was set too low and you have another icon on the screen that looks almost (more than 0.7) like another icon.

Give that a try.

Revision history for this message
GZ (g-zr) said :
#6

Thanks for all the help Eugene S!

Sorry I've just noticed that I forgot to mark it as solved. :)