how do you loop steps?

Asked by Ted Ho

Hi, I am just trying to loop a series of 120 or so steps so that they keep repeating. I want it to go back to step one after it finishes. How do I do this? I've read the other posts about loops and I don't understand what the loop code is. I have no experience in Python besides the simple button pressing I do in Sikuli! Please help me!

Thanks.

Also, is there a way to have it type sequential numbers in text? For example the first time I want it to save a file as abcd1, then abcd2, abcd3 and so on. Can you make it type sequential numbers?

Thanks!

Question information

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

the general form is:

while condition: # loops as long condition evaluates to True
   # some statements inside loop
   # indented by the same number of blanks or tabs

# next statement after loop at indentation level of while

--- your example could look like this together with you numbering option:

here I use 3 blanks to indent one level, in the IDE you should use one tab to indent. the edit menu contains support for indenting/dedenting selected lines together.

i = 1
while True: # loop for ever
   # your statements (steps I guess)
   fileName = "abcd" + str(i)
   # use file name for whatever you want
   i += 1
   if i == 100: break # we make 100 loops max

popup("The End!")

When using While True:, the loop should contain an if ...: break like shown (could be an < if not exists(some-image-that-should-be-there, 0): break > so you can break the loop by making the image invisible somehow.)
Otherwise you have to abort the IDE.

--- alternative 1:
i = 1
while i < 100: # loop would stop if i == 100
   # your statements (steps I guess)
   fileName = "abcd" + str(i)
   # use file name for whatever you want
   i += 1
   if i == 100: break # we make 100 loops max

--- alternative 2:
for i in range(1, 100): # loop would stop if i == 100
# i at start is set to 1 and is incremented by 1 every loop
   # your statements (steps I guess)
   fileName = "abcd" + str(i)
   # use file name for whatever you want

More information available on the official Python website.

Can you help with this problem?

Provide an answer of your own, or ask Ted Ho for more information if necessary.

To post a message you must log in.