简体   繁体   中英

if Else statement inside for loop is not working

am using following code to check certain conditions

 for myarg in myargs:
    if myarg=='heading.png':
       t = find(dirpath + '\\' + myarg)
      t1= capture(t.getX() - 50, t.getY() + 50, t.getW(), t.getH())
      click(t1)

    else
    print "else inside-----------"

myargs conatians heading.png,name.png etc

while executing the above code am gettign erros IndentationError: unindent does not match any outer indentation level

Fix your indentation, you're missing a colon after else and the following print -statement is not indented.

for myarg in myargs:
    if myarg=='heading.png':
        t = find(dirpath + '\\' + myarg)
        t1= capture(t.getX() - 50, t.getY() + 50, t.getW(), t.getH())
        click(t1)

    else:
        print "else inside-----------"

I advice using pylint or a similar syntax checker to avoid problems like this.

You can easily from your error that there is indentation error.
Change your function to

for myarg in myargs:
    if myarg=='heading.png':
        t = find(dirpath + '\\' + myarg)
        t1= capture(t.getX() - 50, t.getY() + 50, t.getW(), t.getH())
        click(t1)
    else:      # here you must use colon
        print "else inside-----------"

Indentation in Python is, simply put, the equivalent of {} in other languages like C++ and C# .

Don't use text editors to write scripts, you can use rich editors for writing scripts.

   for myarg in myargs:
        if myarg=='heading.png':
              t = find(dirpath + '\\' + myarg)
              t1= capture(t.getX() - 50, t.getY() + 50, t.getW(), t.getH())
              click(t1)
       else:
              print "else inside-----------"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM