mercredi 29 août 2018

How to select a random object except one spesific one from a list?

I am designing an experiment with the Expyriment package. There is two boxes and I give them random colors but they must be different from each others. I use for loop and enumerate to loop through:

color = [(0, 76, 153), (204, 0, 0), (0, 153, 0), (255, 230, 0)]

for i, x in enumerate(color): print(color[i]) print(random.choice(color.remove(color[i])))

2 problems emerged from here, the tuple I removed gone forever and I got a TypeError: object of type 'NoneType' has no len()

I search for a way to temporarely remove an tuple for looping but cant find it.

Also, when I tried using pop [ print(random.choice(color.pop(i))) ], it didnt give me an error but it just prints int not tuple. Also, object that I pop is still gone forever.

Sorry about the mistakes, this is my first post. Thank you.




Aucun commentaire:

Enregistrer un commentaire