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
...
>>> 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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL 自定義函式FUNCTIONSQL函式Function
- js 函式報錯“Uncaught: TypeError xxx is not a function”JS函式ErrorFunction
- python如何定義函式Python函式
- python---函式定義Python函式
- 什麼是Python函式?如何定義函式?Python函式
- Can't use function return value in write context 使用empty遇到報錯FunctionContext
- Python如何定義一個函式Python函式
- Python巢狀定義函式增強reduce()函式功能Python巢狀函式
- Python騷操作:動態定義函式Python函式
- 函式(FUNCTION)函式Function
- shell 函式定義函式
- appium tap 報錯:outside of element rectAPPIDE
- 【python官方文件】深入理解python函式定義Python函式
- ThinkPHP函式提示錯誤function undefined的方法PHP函式FunctionUndefined
- JavaScript function 函式JavaScriptFunction函式
- 定義函式物件 (轉)函式物件
- 02_函式定義及使用函式函式
- 如何在函式內部定義函式?函式
- PostgreSQL函式裡呼叫函式(SETOF + RETURN QUERY)SQL函式
- python學習總結之 函式定義defPython函式
- python函式的定義和呼叫是什麼?Python函式
- python 自定義函式Python函式
- 兄弟連go教程(11)函式 - 函式定義Go函式
- oracle function函式castOracleFunction函式AST
- JS:1.3,函式(function)JS函式Function
- 在jQuery定義自己函式jQuery函式
- mysql函式定義語法MySql函式
- qt之函式重定義QT函式
- Python進階 函式快取 (Function caching)Python函式快取Function
- 第 8 節:函式-函式定義和引數函式
- 函式引數 引數定義函式型別函式型別
- 函式定義、函式的引數、函式的預設引數函式
- makefile--函式定義與呼叫函式
- sql server檢視函式定義SQLServer函式
- oracle 過程函式定義字典Oracle函式
- Java 8 Function 函式介面JavaFunction函式
- js的函式function(一)JS函式Function
- javascript Function()建構函式JavaScriptFunction函式