【问题标题】:Operator error while trying to change the value of a key in python [duplicate]尝试更改python中键的值时出现运算符错误[重复]
【发布时间】: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

预期目的:

  1. 第一个集合生成一个包含 30 个字典的嵌套列表

  2. 然后我们将前三个外星人改为黄色

  3. 我们打印前 5 个外星人

  4. 现在将所有黄色外星人更改为红色,将绿色外星人更改为黄色

  5. 打印所有外星人

【问题讨论】:

  • 删除and并将and后面的行移到下一行
  • 只需将其分成两行,而不是尝试在一个语句中使用and?!
  • 请问为什么会这样?我的意思是,根据我的说法,带有“and”语句的方法也应该可以正常工作。
  • 否,并且运算符用于条件语句中,而不是同时进行两个赋值。把它分成两行。我建议看一些 python 教程左右来了解基础知识。
  • Assignments 是 Python 中的 statements,你不能用 and 链接语句。

标签: python list dictionary nested operators


【解决方案1】:

在更新dictionaryalien_x期间删除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}]

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 2014-03-18
    • 2021-10-29
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多