lundi 8 juin 2020

Python Random Choice Returning Same Result in Every Loop Iteration

Anyone know why when I run this code I get the same dictionary entry appended 100 times?

from random import choice

aliens = []
alien = {}

colors = ['red', 'blue', 'green', 'black', 'purple', 'brown', 'yellow', 'coral']
points = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
speeds = ['slow', 'medium', 'fast']

for i in range(1,101):
    alien['color'] = choice(colors)
    alien['points'] = choice(points)
    alien['speed'] = choice(speeds)
    aliens.append(alien)

print(aliens)



Aucun commentaire:

Enregistrer un commentaire