問題描述
SyntaxError:無效語法(打印功能) (SyntaxError: invalid syntax (print function))
>>> x=[]
>>> for i in range(10):
x.append(i)
print(x)
SyntaxError: invalid syntax
我正在使用 Python 3.5。當它不是循環的一部分時,我似乎無法讓打印功能正常工作。“print”被突出顯示為語法錯誤的來源,但我似乎無法找出錯誤的原因。它在 while 或 for 循環的一部分時打印得非常好。這可能是由一個非常愚蠢的疏忽造成的,如果有人能指出這一點,我將不勝感激。
參考解法
方法 1:
If you are typing in console, you need to hit enter twice to end that statement. Which in your case, what you did is, you wrote print inside for without indentation. So it will show syntax error.
(by rlrq、Sidharth Patnaik)