天天看点

Python定义函数报错 return outside function

在Python中定义函数时报错  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

报错原因:

函数中的缩进格式有误,第3行之后的缩进格式不正确

解决方法:

规范缩进格式

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

... 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26506993/viewspace-2141634/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26506993/viewspace-2141634/