Python 变量更改会给我带来语法错误“;Can';t分配给操作员“;

Python 变量更改会给我带来语法错误“;Can';t分配给操作员“;,python,Python,我正在一个名为“repl.it”的站点上用Python编写一个基于文本的游戏,在尝试更改变量时,我遇到了以下错误: Traceback (most recent call last): File "python", line 526 SyntaxError: can't assign to operator 我是python的新手,我不太懂,所以我只希望有人修复代码并告诉我这是如何工作的 代码如下: #Adding hits or misses if decision == "t3" :

我正在一个名为“repl.it”的站点上用Python编写一个基于文本的游戏,在尝试更改变量时,我遇到了以下错误:

Traceback (most recent call last):
  File "python", line 526
SyntaxError: can't assign to operator
我是python的新手,我不太懂,所以我只希望有人修复代码并告诉我这是如何工作的

代码如下:

#Adding hits or misses
if decision == "t3" : mothershiphit = mothershiphit + 1
elif decision == "t2" : jetdown = jetdown + 1
else: mothershipmiss = mothershipmiss + 1, mothershiplanded = mothershiplanded + 1

print " "
它还没有完成,但我会继续工作。

使用分号(;)将语句分隔成一行,而不是使用逗号(,)

if decision == "t3": mothershiphit = mothershiphit + 1
elif decision == "t2": jetdown = jetdown + 1
else: mothershipmiss = mothershipmiss + 1; mothershiplanded = mothershiplanded + 1
解释

使用逗号,解释器会认为它在执行以下操作:

mothershipmiss + 1, mothershiplanded = mothershiplanded + 1
正如您所看到的,在第一行中,您实际上是将+1添加到运算符(左侧的内容),这是无效的

使用分号时,语句将改为如下所示:

mothershipmiss = mothershipmiss + 1
mothershiplanded = mothershiplanded + 1

这是有效的,因为您将1指定给右侧的元素。

更好的是:使用多行。是的,我同意您的看法。我没有在回答中写这句话,因为这不是用户要求的,但它肯定会让代码更具可读性。