Python运算符(+ =)和SyntaxError

Cu *_* Hh 6 python operators syntax-error python-2.7

好的,我做错了什么?

x = 1

print x += 1
Run Code Online (Sandbox Code Playgroud)

错误:

print x += 1
         ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

或者,+=不再适用于Python 2.7?我会发誓我过去曾经用过它.

Ana*_*mar 12

x += 1是Python中的扩充赋值语句.

你不能在print语句中使用语句,这就是你得到语法错误的原因.你只能在那里使用表达式.

你可以做 -

x = 1
x += 1
print x
Run Code Online (Sandbox Code Playgroud)