-2

I am not able to understand why is line 22 an operator error

My Code:

#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)

error:

alien_x['colour'] = 'yellow' and alien_x['points'] = alien_x['points'] + 10
                    ^
SyntaxError: cannot assign to operator

intended purpose:

  1. first set generates a nested list of 30 dictonaries

  2. then we change first three aliens to yellow

  3. we print the first 5 aliens

  4. now change all yellow aliens to red and green aliens to yellow

  5. print all aliens

  • Remove the ```and``` and move the line following the ```and``` to the next line – Sushil Oct 20 '20 at 08:39
  • 1
    Just separate that into two separate lines instead of trying to do it in one statement with `and`?! – deceze Oct 20 '20 at 08:40
  • May I please know why this is happening? I mean the method with "and" statement should work just fine too according to me. – Very Useless Oct 20 '20 at 08:53
  • no, and operator is used in condtional statements, not to make two assignments at the same time. Just make it in two lines. I would recommend looking at some python tutorial or so to understand the basics. – kspr Oct 20 '20 at 09:10
  • Assignments are *statements* in Python, you cannot chain statements with `and`. – deceze Oct 20 '20 at 09:11

1 Answers1

0

Remove the and during updating the dictionary alien_x

Correction Made:

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

Output

[{'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}]
KittoMi
  • 442
  • 2
  • 16