← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #168610]: Loop script, tried but can't understand it

 

Question #168610 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/168610

    Status: Open => Answered

RaiMan proposed the following answer:
Congratulations.

--- Tip: if you want to comment your script, just put a sort action name behind the statement like
click () #action1

so you can tell e.g.

when my script has reached the end, I want to start all over again at
#action1

--- break things up in smaller pieces
To have a better overview over your workflow, it helps, from start to use references to pieces of code, that are rather stable and that live somewhere else in the script: functions. The functions are defined with def a_name(): followed by the code, that should be processed, when this function is called later on using a_name(). So the same piece of code can be reused at different places all over again, even with different parameters.

your code revised with functions:

def refuelFleet() # This refuels your fleet
    click( )
    wait(2)
    click( )
    wait(2)
    click( )
    wait(2)
    click( )
    wait(2)

def startInstances() # This starts your Instances
    click( )
    wait(2)
    click( )
    wait(2)
    click( )#Instance Lvl
    wait(2)
    click( )
    wait(2)
    click( )#Fleet1
    wait(1)
    click( )#Fleet2
    wait(1)
    click( )#Fleet3
    wait(1)
    click( )#Fleet4
    wait(1)
    click( )#Fleet5
    wait(1)
    click( )#Fleet6
    wait(2)
    click( )
    wait(2)
    click( )
    wait( , FOREVER)
    click( )

def sortMail() #This will sort Instances mail and box
    click( )
    wait(2)
    click( )
    wait(2)
    click( )
    wait(2)
    click( )
    wait(2)
    click( )
    wait(3)

# This is your workflow with no changes
while exists( , 0):
    break # but ends if img gets visible
    popup("UNDER ATTACK!!!")
while not exists( , 0):
    click( )# This makes sure your in Space Base
    wait(2)
    refuelFleet() # This refuels your fleet
    startInstances() # This starts your Instances
    sortMail() #This will sort Instances mail and box

# concentrating on the workflow

now the only piece of code we have to deal with, to solve the workflow
problem:

# This is your workflow
while exists( , 0):
    break # but ends if img gets visible
    popup("UNDER ATTACK!!!")
while not exists( , 0): # looking for image1
    # section1
    click( )# This makes sure your in Space Base
    wait(2)
    refuelFleet() # This refuels your fleet
    startInstances() # This starts your Instances
    sortMail() #This will sort Instances mail and box
    # section1 end

I guess once more: 
- as long as image1 is not visible, perform section 1
- if image1 gets visible, do section2
- after section2 is done, start all over again

I try:

while True: # loops forever
    # label1
    if exists( , 0): # looking for image1
        #section 2
        pass # a no operation to keep indenting syntax
        # replace with whatever code you want
        # section2 end
    # section1
    click( )# This makes sure your in Space Base
    wait(2)
    refuelFleet() # This refuels your fleet
    startInstances() # This starts your Instances
    sortMail() #This will sort Instances mail and box
    # section1 end
    if some_condition: break # breakout
    # label2

# here script will continue if break is processed

comment:

1. script will start at label1 and process the statements till label2
2. then it will start again at label1
3. this will happen forever (while True:), except one of the finds in the clicks fail (script stops with FindFailed)

statement breakout:
some_condition has to be substituted by an expression, that evaluates to either True or False (like e.g. exists(image)). If True, it will end the loop, if False, break is not processed.

if True: break # will end the loop in any case
if False: break# will never end the loop

examples for conditions:
- some image is visible
- a certain time of day is reached
- a counter or timer has reached a threshold value
- many more ...

If you have only one break in the while loop, the condition might replace the True in while True:, since the while statement generally is:
while some_condition:
where some_condition is evaluated, whenever processing reaches the while statement.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.