ITPub博客

首页 > 应用开发 > IT综合 > Python定义函数报错 return outside function

Python定义函数报错 return outside function

原创 IT综合 作者:feelpurple 时间:2017-07-05 23:23:20 0 删除 编辑
在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博客 ” ,链接:https://blog.itpub.net/26506993/viewspace-2141634/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-08-01

  • 博文量
    404
  • 访问量
    1673942