jeudi 11 janvier 2024

How do I get a random choice from a list to not give me duplicates?

Sorry if it's unclear, beginner here. Basically I have this to pick random questions for a quiz:

import random

econ = ["eq1", "eq2", "eq3", "eq4", "eq5", "eq6", "eq7", "eq8"]
mil = ["mq1", "mq2", "mq3", "mq4", "mq5", "mq6", "mq7", "mq8"]
oro = ["oq1", "oq2", "oq3", "oq4", "oq5", "oq6", "oq7", "oq8"]
pol = ["pq1", "pq2", "pq3", "pq4", "pq5", "pq6", "pq7", "pq8"]

cats = [econ,mil, oro, pol]
 
for x in cats:
        for y in range(3):
            print(random.choice(x))

However, I sometimes get duplicates, example: eq3 <- here eq3 <- here eq1 mq5 mq7 mq2 oq1 oq4 oq5 pq3 pq7 pq2

I've scoured through forums and the python coding book but was unable to find something that didn't confuse me more!

TL;DR: How do I avoid repetition of random items in the code?




Aucun commentaire:

Enregistrer un commentaire