5

So far what I have is

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 output ends up beings something along the lines of

e
e
c
b
e

How do I make it so a number is only used once and the letters do not repeat? (ex. something like)

a
e
b
c
d

Thanks in advance

2
  • 3
    you can try random.sample("abcde", 5) random library python 3 Commented Sep 15, 2015 at 22:37
  • 1
    your specific code example suggests that you want L = list("abcde"); random.shuffle(L) i.e., get a random permutation of "abcde" letters. Commented Sep 15, 2015 at 23:37

1 Answer 1

9

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

r = random.sample("abcde", 5)
for element in r:
    print(element)
Sign up to request clarification or add additional context in comments.

5 Comments

@zeurosis if it worked for you please select it as the accepted answer
sorry how do I do that? I haven't used this website a whole lot
@zeurosis click the check mark under the upvote/downvote icons on the left of the post
@zeurosis you might want to do that on your previous questions as well ;)
random.sample("abcde", 5) is a weird way to emulate random.shuffle()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.