2014-04-01 51 views
0

我用下面的代碼else語句檢查某些條件如果在for循環中不能正常工作

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在執行上面的代碼時

gettign誤差修改IndentationError: unindent does not match any outer indentation level

+2

在你的問題中,你的縮進在很多地方都是錯誤的。 – sshashank124

+0

在Python中,一切都必須正確縮進,因爲錯誤告訴你。 'if'塊之後的所有內容應該處於相同的縮進位置,直到else塊,此時'else'之後的塊應該等同於'if'塊 – ydaetskcoR

+1

':'在每個塊初始化之後被請求,包括'else' – Trimax

回答

8

解決您的壓痕,您在else和以下print之後缺少冒號 - 聲明不縮進。

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-----------" 

我建議使用pylint或類似的語法檢查器來避免這樣的問題。

0

你可以很容易地從你的錯誤,有縮進錯誤。
你的函數改爲

 
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-----------" 
0

縮進Python是,簡單地說,的{}C++C#其他語言的等價物。

不要使用文本編輯器編寫腳本,你可以使用豐富的編輯器編寫腳本。

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-----------"