您好,登錄后才能下訂單哦!
Python中定義函數(shù)時出現(xiàn) return outside function報錯如何解決,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
在Python中定義函數(shù)時報錯 SyntaxError: 'return' outside function
>>> def testPass(cryptPass):
... salt = cryptPass[0:2]
... dictFile = open('dictionary.txt', 'r')
File "", line 3
dictFile = open('dictionary.txt', 'r')
^
SyntaxError: invalid syntax
>>> for word in dictFile.readlines():
... word = word.strip('\n')
... cryptWord = crypt.crypt(word,salt)
... if (cryptWord == cryptPass):
... print "[+] Found Password: "+word+"\n"
... return True
... print "[-] Pasword Not Found.\n"
... return False
...
File "", line 6
SyntaxError: 'return' outside function
報錯原因:
函數(shù)中的縮進格式有誤,第3行之后的縮進格式不正確
解決方法:
規(guī)范縮進格式
>>> def testPass(cryptPass):
... salt = cryptPass[0:2]
... dictFile = open('dictionary.txt', 'r')
... for word in dictFile.readlines():
... word = word.strip('\n')
... cryptWord = crypt.crypt(word,salt)
... if (cryptWord == cryptPass):
... print "[+] Found Password: "+word+"\n"
... return
... print "[-] Pasword Not Found.\n"
... return
...
關(guān)于Python中定義函數(shù)時出現(xiàn) return outside function報錯如何解決問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。