2

Have nice day.

Here's my problem. I have 8 color and i want to select randomly 4 different color. I randomly selected 4 color but colors are repeating. How can i fix and get better my code?

My code:

import random
r = "Red"
o = "Orange"
y = "Yellow"
g = "Green"
b = "Blue"
p = "Purple"
m = "Maroon"
u = "Ultramarine"

liste = [r,o,y,g,b,p,m,u]
liste2 = [random.choice(liste) +  random.choice(liste) + random.choice(liste) + random.choice(liste)]
print(liste2)

1 Answers1

3

You can use random.sample:

liste2 = random.sample(liste, 4)
print(liste2)

Output:

['Ultramarine', 'Green', 'Blue', 'Purple']
iz_
  • 15,923
  • 3
  • 25
  • 40