Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试更改python中键的值时出现运算符错误_Python_List_Dictionary_Nested_Operators - Fatal编程技术网

尝试更改python中键的值时出现运算符错误

尝试更改python中键的值时出现运算符错误,python,list,dictionary,nested,operators,Python,List,Dictionary,Nested,Operators,我不明白为什么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['

我不明白为什么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语句的方法应该也能很好地工作。不,and运算符用于条件语句,而不是同时做两个赋值。就分成两行。我建议您阅读一些python教程来了解基础知识。赋值是python中的语句,不能将语句与and链接起来。
for alien_x in aliens :
    if alien_x['colour'] == 'yellow' :
        alien_x['colour'] = 'red'
        alien_x['points'] = alien_x['points'] + 10

    elif alien_x['colour'] == 'green' :
        alien_x['colour'] = 'yellow'
        alien_x['points'] = alien_x['points'] + 10
[{'colour': 'yellow', 'points': 10}, {'colour': 'yellow', 'points': 10}, {'colour': 'yellow', 'points': 10}, {'colour': 'green', 'points': 5}, {'colour': 'green', 'points': 5}]
no, of aliens is 30
[{'colour': 'red', 'points': 20}, {'colour': 'red', 'points': 20}, {'colour': 'red', 'points': 20}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}, {'colour': 'yellow', 'points': 15}]