code to make 30 aliens,to add values in a list using append
alien = []
for alien_number in range(30):
new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
aliens.append(new_alien)
print('The first 10 aliens are:')
print("\t")
for alien in aliens[:5]:
if alien['color'] == 'green':
alien['color'] = 'yellow'
alien['points'] = 10
alien['speed'] = 'medium'
elif alien['color'] == 'yellow':
alien['color'] = 'red'
alien['points'] = 15
alien['speed'] = 'fast'
# showing the first 10 aliens
for alien in aliens[:10]:
print(alien)
print('....')
# to show how many aliens have been created
print("\n")
print(f'The total number of aliens are:{len(aliens)}')
Expected result which i wanted,Please help me what i have to do to get this result
The first 10 aliens are:
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'red', 'points': 15, 'speed': 'fast'}
{'color': 'red', 'points': 15, 'speed': 'fast'}
{'color': 'red', 'points': 15, 'speed': 'fast'}
{'color': 'red', 'points': 15, 'speed': 'fast'}
{'color': 'red', 'points': 15, 'speed': 'fast'}
....
The total number of aliens are:30
CodePudding user response:
You created an array with data that cannot fit under all conditions. you need to create other data for the rows between 5 and 10 in your table.
Also you didn't go through all the first 10 rows with this condition : for alien in aliens[:5]:
Correct also alien = [] to aliens = []
You can use this code to get your result :
aliens = []
for alien_number in range(30):
if ( alien_number in range(5,10)):
new_alien = {'color': 'yellow', 'points': 5, 'speed': 'slow'}
else:
new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
aliens.append(new_alien)
for alien in aliens[:10]:
if alien['color']=='green':
alien['color']='yellow'
alien['points']=10
alien['speed'] ='medium'
elif alien['color']=='yellow':
alien['color']='red'
alien['points']=15
alien['speed']='fast'
#showing the first 10 aliens
for alien in aliens[:10]:
print(alien)
print('....')
#to show how many aliens have been created
print(f'The total number of aliens are:{len(aliens)}')
Output :
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'red', 'points': 15, 'speed': 'fast'}
{'color': 'red', 'points': 15, 'speed': 'fast'}
{'color': 'red', 'points': 15, 'speed': 'fast'}
{'color': 'red', 'points': 15, 'speed': 'fast'}
{'color': 'red', 'points': 15, 'speed': 'fast'}
....
The total number of aliens are:30
CodePudding user response:
I think there is a typo in the first line of your code, where you have defined alien
instead of aliens
aliens = []
for alien_number in range(30):
new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
aliens.append(new_alien)
...