[HowTo] repeat something in a Sikuli script (make loops)

Created by RaiMan on on 2011-01-14
Keywords:
loop repeat list array
Last updated by:
RaiMan on on 2011-06-24

You have some lines of code in your script, that you want to repeat.

These are the Python loop constructs (most often used):

-- 1. while <some condition>:
The following statements are repeated as long as the expression <some condition> evaluates to True. Everytime, after the last statement in the loop is processed, the <some condition> is evaluated. If False, the loop ends.

-- 2. for entry in entries:
The variable entries must be a list of values. The statements inside the loop are repeated for every entry in this list in ascending order.

-- 3. break and continue
Using statement break in a loop will end the loop at this point. Using statement continue in a loop will instantly start the next turn of the loop (if any). In both cases remaining statements are skipped.

--- The statements inside the loop (the statements on the following lines after the line with the while: for: that you want to be repeated) have to be indented one level. The end of the loop is before the first following statement with the same indentation level as the while:/for: statements.

When working with the Sikuli IDE it is strongly recommended to use Tab and Shift-Tab for indenting/dedenting, since indentation errors are hard to find. (In the following examples 3 blanks are used as one indentation level, because Tab's cannot be used here)

--- Examples:

comment on using pass:
since tokens like while: for: if: elif: else: try: except: expect the following statement to be indented one level, one uses statement pass, which acts like a placeholder and does simply nothing besides being indented, in cases where you do not know yet what to do.

# --- this loops forever
while True:
   if not exists(img, 0):
      break # but ends if img gets visible
   else:
      pass # we will see
   wait(5) # check every 5 seconds
print img, "finally came up"

# --- this loops as long as img is visible
while exists(img, 0):
   click getLastMatch()
   if exists(img1):
      continue # skip the rest and start next turn
   type("some text"+Key.ENTER)
   wait(3)

--- If you know things like "for i=1 step 1 until i=100" or similar constructs: Python does not have such a thing ;-)
You use a construct of type 2 (loop with the entries of a list) with range().

--- range() is a built_in function, that returns a list containing the integer numbers according to the given parameter.
general usage: range(startvalue, endvalue [, stepvalue]) or range(endvalue) which implies start = 0 and step = 1. The last value is always endvalue -1 (max in case of using stepvalue) !!

# --- this loops n times
for n in range(10): # 10 times, n will be 0 to 9
   pass

# --- this loops for the values 1, 3, 5, 7 and 9
for n in range(1, 10, 2):
   pass

# --- loop through a list
myListFixed = (11, 22, 33, 44)
for entry in myListFixed:
   print entry
   lastEntry = entry
   while not exists(img, 0):
      wait(1) # we wait for the entry field
   type(getLastMatch(), e+Key.ENTER)
   wait(3)
   # now the next entry
print "the last entry was:", lastEntry

--- comment: the entries in myListFixed and the list itself cannot be changed (immutable, called tuple). If you want a list, whose entries and the list itself can be changed you have to use
myList = [ 11, 22, 33, 44 ] # this list is mutable and is called a list ;-)

# --- make a list from a tuple
myListFixed = ( 11, 22, 33, 44 )
myList = [] # an empty list
for entry in myListFixed:
   myList.append(entry) # add entry to the end of the list
print myList

# --- change the entries of a list
myList =[ 1, 2, 3, 4 ]
for n in range( len(myList) ): # len() returns the number of entries in the list
   myList[n] += 1 # increment the entry by 1
print list # should be [ 2, 3, 4, 5 ]

# --- nice try: this would NOT change the list entries
for entry in myList:
   entry += 1 # entry is a new object
print list # should still be [ 1, 2, 3, 4 ]

--- If you need more info: the web has many Python tutorials.
We recommend: http://jythonpodcast.hostjava.net/jythonbook/en/1.0/