【发布时间】:2021-02-03 01:29:42
【问题描述】:
我不明白为什么第 22 行是操作员错误
我的代码:
#let's produce 30 aliens
aliens=[]
for alien_number in range(30) :
new_alien = {'colour':'green','points':5}
aliens.append(new_alien)
#changing asome aliens to another type
for alien_x in aliens[:3] :
alien_x['colour'] = 'yellow'
alien_x['points'] = 10
print(aliens[:5])
print(f"no, of aliens is {len(aliens)}")
# game speeds up/ level up
for alien_x in aliens :
if alien_x['colour'] == 'yellow' :
alien_x['colour'] = 'red' and alien_x['points'] = alien_x['points'] + 10
elif alien_x['colour'] == 'green' :
alien_x['colour'] = 'yellow' and alien_x['points'] = alien_x['points'] + 10
print(aliens)
错误:
alien_x['colour'] = 'yellow' and alien_x['points'] = alien_x['points'] + 10
^
SyntaxError: cannot assign to operator
预期目的:
-
第一个集合生成一个包含 30 个字典的嵌套列表
-
然后我们将前三个外星人改为黄色
-
我们打印前 5 个外星人
-
现在将所有黄色外星人更改为红色,将绿色外星人更改为黄色
-
打印所有外星人
【问题讨论】:
-
删除
and并将and后面的行移到下一行 -
只需将其分成两行,而不是尝试在一个语句中使用
and?! -
请问为什么会这样?我的意思是,根据我的说法,带有“and”语句的方法也应该可以正常工作。
-
否,并且运算符用于条件语句中,而不是同时进行两个赋值。把它分成两行。我建议看一些 python 教程左右来了解基础知识。
-
Assignments 是 Python 中的 statements,你不能用
and链接语句。
标签: python list dictionary nested operators