SyntaxError: Missing Parentheses in Call to 'print'
嘗試在Python 3 中使用不帶的print 語句時,您會遇到錯誤「語法錯誤:呼叫中缺少括號'print'"。
原因:
在Python 3 中,print 語句已替換為print() 函數,該函數需要在要列印的值兩邊加上括號.
錯誤用法範例(Python 2語法):
print "Hello, World!"
解決方案:
在 Python 3 中將要列印的值括在括號內。
print("Hello, World!")
歷史:
較早前Python 3 版本中,解釋器報告了一般語法錯誤,沒有具體提示。但是,從Python 3.6.3 開始,錯誤訊息已更新以提供建議的修復:
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello!")?
附加說明:
print() 函數與Python 2的print 語句相比,Python 中的print 語句提供了對輸出格式的更多控制。例如,要將多個項目列印到具有尾隨空格的 stderr,請使用 file=sys.stderr 和 end=" " 參數。
import sys print(1, 2, 3, file=sys.stderr, end=" ") print(4, 5, 6, file=sys.stderr)
以上是為什麼我在 Python 3 中收到「語法錯誤:呼叫『print』時缺少括號」?的詳細內容。更多資訊請關注PHP中文網其他相關文章!