問題描述
我正在嘗試在 Python REPL 上定義一個函數.每次我嘗試運行以下代碼時,都會出現語法錯誤.
代碼:
def hello():打印(你好!")你好()
錯誤:
C:Users~Desktop>pythonPython 3.6.5(v3.6.5:f59c0932b4,2018 年 3 月 28 日,16:07:46)[MSC v.1900 32 位(英特爾)] 在 win32 上輸入幫助"、版權"、信用"或許可"以獲取更多信息.>>>定義你好():...打印(你好!")... 你好()文件<stdin>",第 3 行你好()^SyntaxError:無效的語法
我在 stackoverflow 上遇到的一個可能的解釋是這篇文章
I am trying to define a function on the Python REPL. Every time I try to run the below code, I get a syntax error.
Code:
def hello():
print ("Hello!")
hello()
Error:
C:Users~Desktop>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def hello():
... print ("Hello!")
... hello()
File "<stdin>", line 3
hello()
^
SyntaxError: invalid syntax
A possible explanation I've come across on stackoverflow is this post Python Error : File "<stdin>" where it says I can't run scripts from the Python interpreter. If that's true, why is that the case? I.e. if I'm able to run >>>2+2 in the cmd window, why am I not able to run a 'def' function? Hoping to look for clarification on this point - Thanks!
Edit: Put the error in text, not just the pic.
Press enter once after defining your function (that is, enter one blank line). Essentially, this lets Python know that you are done defining your function.
Once you see >>>
again, you can call your function.
See the picture for how it should look when done right:
這篇關于在 Python 命令行上定義函數時出現語法錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!