← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #160813]: How to use if/elif/else correctly

 

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

    Status: Open => Answered

RaiMan proposed the following answer:
--- first code snippet:

the lines beginning with # (comment) could be deleted

if exists("sikuli-tmp321.png", 0):
 click("OkIlikethepa.png")
 popup("finding clicking like")
else:
 wait("5Like.png") # waits max 3 seconds
 # find("5Like.png") # not needed
 # click("5Like.png") # not needed, it is beeing clicked anyway
popup("sleep for 5") # should be dedented to the first level
sleep(5)
popup("clicking")
click("5Like.png")

--- region.inside()
is redundant: it is simply the region itself, duplicated in a new Region object
generally only enhances readability in some cases

--- second snippet

reg= (region here)
reg.highlight(2) # Just checking if it finds the region

# reg.inside().if.exists( ): # not Python
if reg.exists():
  popup("it exists")#checking if it exists so command
#else:
elif reg.exists("some-image-that-means-it-is-error2.png"):
 click("") #if error 2 is found, clicks skip button for error2
 popup("error2")
else:
 click("") #main function
 popup("doing job") #if no error is found

An if cannot have 2 else, since the if-condition either evals to True or False.
So if you have more outcomes/options/alternatives you either have to nest decisions or make a case construct with if/elif/.../elif/else (like above).

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