if statements inside for loop

Asked by KRP

When used the if construct inside for loop getting the below error
no viable alternative at input 'if'"

for entry in myListFixed:

   while exists(Pattern("1322634310829.png").similar(0.90).targetOffset(-2,1), 0):(click("DDCCY.png"),
(type(Key.DOWN), type("c", KEY_CTRL)),
click("img-1.png"),
exists("Coumry.png"),
doubleClick("Coumry-1.png"),
type("v", KEY_CTRL),
if exists("Gen-1.png"):(click("GenP.png"),
click("OpenPage-2.png"),
     click("Administrato.png"),),
)

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

Revision history for this message
Mikeldi Latorre (magodiez) said :
#1

why don't you try to program in more than one line??? You'll have you code much more legible:

for entry in myListFixed:
    while exists(Pattern("1322634310829.png").similar(0.90).targetOffset(-2,1), 0):
        click("DDCCY.png")
        type(Key.DOWN)
        type("c", KEY_CTRL)
        click("img-1.png")
        exists("Coumry.png")
        doubleClick("Coumry-1.png")
        type("v", KEY_CTRL)
        if exists("Gen-1.png"):
            click("GenP.png")
            click("OpenPage-2.png")
            click("Administrato.png")

Revision history for this message
Calle Rundgren (c-rundgren) said :
#2

You need to indent the code inside the if-statement.

Example:

if exists("img1.png")
    click("img1.png")

This is the proper way to use if-statements in python. Everything who is indented inside the statement will be included.

Revision history for this message
KRP (chittiprasad80) said :
#3

Hi Calle thanks for the reply,

but the problem here is after the If condition the loop is not running,
means the loop ends after the if statement inside the loop, not returning to loop back

if you are not clear about my Q,

For entry in <condtion>:
while exists(img):
do some action,,,,,
if <condition> do some action
#after the above "If" check for while<> again
)

Revision history for this message
RaiMan (raimund-hocke) said :
#4

send me your .sikuli as zipped file to my mail at https://launchpad.net/~raimund-hocke

Revision history for this message
surfdork (surfdork) said :
#5

Try a while loop include an image within the click() or exists or whatever action you want to perform.

Also I don't see any wait statements included with the visual assertions.

count = 0
while (count <10):
    click(), count
    count = count + 1
popup ("finished counting")

Revision history for this message
RaiMan (raimund-hocke) said :
#6

@ KRP
I got you zipped script and had a look at it.

I have rewritten parts of it and solved your loop issue. I have sent it back to you.

Your coding generally works, but does not allow to nest any execution below level 0

--- you code like that

if <some-condition>: (expression, expression, expression, expression, ...., expression)
elif <some-condition>: (expression, expression, expression, expression, ...., expression)
else (expression, expression, expression, expression, ...., expression)

This is syntactically ok. But it is a wast of resources: each expression is evaluated from left to right and finally stored in an anonymous list (tuple), that is not referenced again. And you cannot nest additional if/while/for constructs, because these need to be statements.

--- normal Python scripting

if <some-condition>:
    statement
    statement
    statement
elif <some-condition>:
    statement
    statement
    statement
else:
    statement
    statement
    statement

where each statement can be any valid i/while/else construct:

if <some-condition>:
    statement
    if <some-condition>:
        statement
    else:
        statement
    statement
elif <some-condition>:
    statement
    statement
    statement
else:
    statement
    statement
    statement

The only tricky thing is indentation, which tells Python which statements belong to a block of statements.

So to solve your issue see comment #1 of Mikeldi above, which is fully correct based on the code snippet you gave in your question (I know, that your real script looks different):

for entry in myListFixed:
    while exists(Pattern("1322634310829.png").similar(0.90).targetOffset(-2,1), 0):
        click("DDCCY.png")
        type(Key.DOWN)
        type("c", KEY_CTRL)
        click("img-1.png")
        exists("Coumry.png")
        doubleClick("Coumry-1.png")
        type("v", KEY_CTRL)
        if exists("Gen-1.png"):
            click("GenP.png")
            click("OpenPage-2.png")
            click("Administrato.png")

Can you help with this problem?

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

To post a message you must log in.