Help me build a Python Alien fleet!!!!!

Asked 2 years ago, Updated 2 years ago, 17 views

The first five are

'speed': 'medium', 'color': 'yellow', 'points': 10

The next five are

'speed': 'slow', 'color': 'green', 'points': 5

The next five are

'speed': 'fast', 'color': 'red', 'points': 15

For the rest

'speed': 'slow', 'color': 'blue', 'points': 3

/./

This is the code I made.

>>> alien_0 = {'speed': 'medium', 'color': 'yellow', 'points': 10}
>>> alien_1 = {'speed': 'slow', 'color': 'green', 'points': 5}
>>> alien_2 = {'speed': 'fast', 'color': 'red', 'points': 15}
>>> alien_3 = {'speed': 'slow', 'color': 'blue', 'points': 3}
>>> aliens = [alien_0, alien_1, alien_2, alien_3]
>>> for alien in aliens[0:30] :
    if alien['speed']=='medium' :
            alien['color']='yellow'
            alien['points']=10
    elif alien['speed']=='slow' :
            alien['color']='green'
            alien['points']=5
    elif alien['speed']=='fast' :
            alien['color']='red'
            alien['points']=15
    elif alien['speed']=='slow1' :
            alien['color']='blue'
            alien['points']=3
    print(alien)
{'speed': 'medium', 'color': 'yellow', 'points': 10}
{'speed': 'slow', 'color': 'green', 'points': 5}
{'speed': 'fast', 'color': 'red', 'points': 15}
{'speed': 'slow', 'color': 'green', 'points': 5}

There should be a total of 30 units, but the first/second/third one was printed, and the last one was 15 units, but only one was printed... How can I complete it?

python

2022-09-21 10:09

1 Answers

aliens = [alien_0, alien_1, alien_2, alien_3]
for alien in aliens[0:30] :

There are only four alienes on the list of aliens. So even if you do the for loop from 0 to 30, there are only 4 in the aliens, so only 4 are printed.

fori in range (30): #i value from 0 to 29 (inclusive)
    If 0 <= i < 5: # When i is greater than or equal to 5
        print(alien0) #Print Alien0
    If...: # The following conditions:
        print(...) # Another alias

The range (30) above refers to a list of [0, 1, 2, 3, ..., 29].


2022-09-21 10:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.