0
import random
def create_code(characters):
    return list(random.choice(characters)*4)
if __name__ == '__main__':
    characters = 'ygobpr'
    print create_code(characters)

How to return a list of 4 strings, each random, from the given string characters = 'ygobpr'?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Deka
  • 71
  • 1
  • 3
  • 6

2 Answers2

0

You can create a list of random characters as following:

import random
def create_code(characters):
    return list(random.choice(characters) for n in xrange(4))

if __name__ == '__main__':
    characters = 'ygobpr'
    print create_code(characters)
Dalek
  • 4,168
  • 11
  • 48
  • 100
0

each string should just be one letter out of the string 'ygobpr' –

You can just use random.sample() directly:

In [8]: random.sample(characters, 4)
Out[8]: ['y', 'b', 'o', 'p']
NPE
  • 486,780
  • 108
  • 951
  • 1,012