Can we have sections of codes and can jump while execution?

Asked by Farooq Pathan

Guys,

I need to know if there is any way to create sections of code and to jump while execution

#generate random section to execute
import random
random.seed()
section_to_execute = random.randint(1, 3)

#go to that section
goto section_to_execute

#now here is the code of all sections

#section1
popup("you are in section 1 code")

#section2
popup("you are in section 2 code")

#section3
popup("you are in section 3 code")

So is it possible to select section which we need to execute from a random variable, and only that section should be executed all other code should be skipped.

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

you seem to already know, that Sikuli script "talks" Python language.

So we do not have a "goto".

the available elements to control the workflow of your script:

if/elif/else - make decisions (no case construct available)
while/for - loop as long a condition is met, or for a sequence of elements (look faq)
def - make functions that can be called from elsewhere
try/execpt - catch and handle exceptions

these are the most common ones.

in your case:

#generate random section to execute
import random
random.seed()
section_to_execute = random.randint(1, 3)

#now here is the code of all sections (before any section is run)

if section_to_execute == 1:
    #section1
    popup("you are in section 1 code")
elif section_to_execute == 2:
    #section2
    popup("you are in section 2 code")
elif section_to_execute == 3:
    #section3
    popup("you are in section 3 code")
else:
    # this should not happen in your case
    popup("something went totally wrong")

#now here is the code of all sections (after one of the sections was run).

Mind indentation: use one tab for one indent level and use shift-tab for dedent (works on blocks of selected lines).

Can you help with this problem?

Provide an answer of your own, or ask Farooq Pathan for more information if necessary.

To post a message you must log in.