while not exists() or not exists(): does not seem to work

Asked by GZ

Hi Everyone,

I am trying to do the following that is working when I separate the commands but this way it will keep printing "waiting for pattern", even thought in the second exists() the image is existing.

while not exists(AN IMAGE THAT DOESN'T EXIST,0) or not exists(AN IMAGE THAT EXISTS,0):
    wait(0.01)
    print "waiting for pattern"

If I only do this, than it finds the image as expected:

while not exists(AN IMAGE THAT EXISTS,0):
    wait(0.01)
    print "waiting for pattern"

This is the python equivalent of the top one, wich is working in python, after that is greater than 5.
this = 5
that = 1
while not this>3 or not that>5:
    print this, that
    that = that + 1

What am I missing here, or is it a bug? :)

Thanks,
George

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:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

while not exists(AN IMAGE THAT DOESN'T EXIST,0) or not exists(AN IMAGE THAT EXISTS,0):

will loop until both images exist, because as long as either of the images does not exist, <not exists()> is true and hence
the complete or is true

false or false is false
true or false is true
false or true is true
true or true is true

and your example is like a self fulfilling prophecy ( not this>3 is always false)

your example made more real and comparable:
this = 1
that = 2
while not this>3 or not that>5:
    print this, that
    that += 1
    this += 1
    print "not this>3", not this>3
    print "not that>5", not that>5

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

With the python exampl I was trying to demonstrate what it would go through to my mind in SikuliX.

this = 5 # this is for the image that will never exist
that = 1 # this is for the image that is not yet on screen but I always add 1 to it and when it reaches 5 it "appears on screen"
while not this>3 or not that>5:
    print this, that
    that = that + 1

Basically there are two possible images that can show up.
It should wait until either of them shows up, and when it does it should get past the loop.

What would be the best way to do that?

Revision history for this message
RaiMan (raimund-hocke) said :
#3

what you want is more something for an until (but this is not available in Python).

until exists image1 or exists image2:
     wait(someTime)

or in words with a while:
while neither image1 nor image2 are visible just wait a bit and look again

with a while and condition:

while not exists(image1, 0) and not exists(image2, 0):
    wait(1)

as a simulated until:

while True:
    if exists(image1, 0) or exists(image2, 0):
        # do something
        break
    wait(1)

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

Thank you RaiMan that is working!

But how come that your example is working and mine with the simple while is not?

So this is working:

while True:
    if exists(image1, 0) or exists(image2, 0):
        # do something
        break
    wait(1)

But this is only working if image1 is existant, but if image1 is not existant only image2, then it just keeps waiting in the loop.

while not exists(image1, 0) or not exists(image2, 0):
    wait(1)

I remember you said that:
false or false is false
true or false is true
false or true is true
true or true is true

But wouldn't that be the case for the if statement? Or is that different because of the True?

Revision history for this message
RaiMan (raimund-hocke) said :
#5

I remember you said that:
false or false is false
true or false is true
false or true is true
true or true is true

this is for an or

like your
while not exists(image1, 0) or not exists(image2, 0):
    wait(1)

shorter:
while condition1 or condition2:
    wait(1)

where
condition1 is not exists(image1, 0)
condition2 is not exists(image2, 0)

the while loops as long as
 condition1 or condition2
evaluates to True

the last 3 cases in the little logic table.

hence the while only stops running if
 condition1 or condition2
evaluate to False

which is only the case if BOTH condition1 AND condition2 evaluate to False, which is only the case, if BOTH images are there.

--- But wouldn't that be the case for the if statement?
yes, the if statement has the same logic, but condition1 and condition2 are the opposite compared to the while, since we want to check wether we have to END the loop.
the while condition tells, wether the loop should continue to run.

Revision history for this message
RaiMan (raimund-hocke) said :
#6

Uuups, just realised the catastrophic cut&paste typo in comment #4.

So if you only want to wait until one of the images is visible:

while not exists(image1, 0) and not exists(image2, 0):
    wait(1)

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

Ok, I see. Thanks RaiMan!!

I was using the same but with "or".

So I thought that I have to use "and" if both have to be fulfilled and "or" if one of them is enough!?

To me, this would mean that it will exit the loop only if image1 and image2 are not visible:
while not exists(image1, 0) and not exists(image2, 0):
    wait(1)
But it is acting like I would expect "or" to act and "or" acts like I would expect "and" to work.

Sorry if it just me thinking in a weird way today? :)

Revision history for this message
RaiMan (raimund-hocke) said :
#8

--- So I thought that I have to use "and" if both have to be fulfilled and "or" if one of them is enough!?

yes, exactly.

But in program logic one has also take into account, what should happen in what case.
And with while it is so, that it runs until the condition (how complex it might be) becomes False.

Whereas the if condition inside the while-True loop, runs the break, if the condition is True.

Nevertheless: I am rather sure, that "misbehaving" complex conditions are one of the most causes for bugs (... and this is true for SikuliX too ;-)

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

Haha I see.

Thanks again RaiMan.

As always, you have been a great help! :)