How to create non-repeating random numbers in a while loop? (Python 3)

I still have

import random def go(): rounds = 0 while rounds < 5: number = random.randint(1, 5) if number == 1: print('a') elif number == 2: print('b') elif number == 3: print('c') elif number == 4: print('d') elif number == 5: print('e') rounds = rounds + 1 go() 

and the exit ends with creatures something along the lines

 e e c b e 

How to make sure that the number is used only once and the letters are not repeated? (for example, something like)

 a e b c d 

Thank you in advance

+5
source share
1 answer

The random.sample (population, k) method returns a sample of unique values ​​from a given population of length k.

 r = random.sample("abcde", 5) for element in r: print(element) 
+9
source

All Articles