qwerteee123
qwerteee123

Reputation: 1

Can't use random.choice() function on a list?

So I'm trying to make a random string generator, of four characters which goes as a consonant, a vowel, a consonant, and then a vowel. I don't want any consonants to be used more than once. etc. kajo or qyzu. However, I'm having trouble with the random.choice() method which is not expected. When I run the program, I get the below error:

  File "C:\User\Documents\Coding\Python\string_generator.py", line 6, in <module>
    i = random.choice(consonants)
  File "C:\User\AppData\Local\Programs\Python\Python39\lib\random.py", line 346, in choice
    return seq[self._randbelow(len(seq))]
IndexError: list index out of range

My code is below:

import random

vowels = 'aeiouy'
consonants = ['b','c','d','f','g','h','j','k','l','m','m','n','p','q','q','r','s','t','v','v','v','v','w','x','x','x','x','z','z','z','z']
for num in range(20):
    i = random.choice(consonants)
    word = i
    for x in consonants:
        if x == i:
            consonants.remove(x)
    word += random.choice(vowels)
    i = random.choice(consonants)
    word += i
    for x in consonants:
        if x == i:
            consonants.remove(x)
    word += random.choice(vowels)

Upvotes: 0

Views: 470

Answers (3)

Berzohr
Berzohr

Reputation: 416

I think you have a lot of for in your code.

What's string you need to expected at the end?

If you need a string long 20 chars, with unique consonant taken from your array, you need write like this:

import random

word = "";
vowels = 'aeiouy'
consonants =['b','c','d','f','g','h','j','k','l','m','m','n','p','q','q','r','s','t','v','v','v','v','w','x','x','x','x','z','z','z','z']
for num in range(20):
    i = random.choice(consonants)
    consonants.remove(i)
    word += random.choice(vowels)
    word += i

print (word)

In this case the random string is composed like this pattern: (vowel+consonant)*20

Output:

utivecojyvuluzavomuxydimisokeziwanohubyx

Upvotes: 0

Berzohr
Berzohr

Reputation: 416

If you need 20 times a random of 4 characters with this pattern: consonant+vowel+consonant+vowel, this is the solution:

import random

vowels = 'aeiouy'
consonants =['b','c','d','f','g','h','j','k','l','m','m','n','p','q','q','r','s','t','v','v','v','v','w','x','x','x','x','z','z','z','z']
for num in range(20):
    consonantsCopy = consonants.copy()
    firstConsonant = random.choice(consonantsCopy)
    consonantsCopy.remove(firstConsonant)
    secondConsonant = random.choice(consonantsCopy)
    consonantsCopy.remove(secondConsonant)

    tempString = firstConsonant + random.choice(vowels) + secondConsonant + random.choice(vowels)
    print(tempString)

Output:

sufa
quzu
zela
bixo
beju
mafy
vybe
zumo
kozo

Upvotes: 0

ChrisOram
ChrisOram

Reputation: 1434

As juanpa.arrivillaga mentioned, the list is empty on one of the calls to random.choice(consonants) in your for loop. You can see this by adding print(consonants) as the first line within the loop:

Output:

['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'm', 'n', 'p', 'q', 'q', 'r', 's', 't', 'v', 'v', 'v', 'v', 'w', 'x', 'x', 'x', 'x', 'z', 'z', 'z', 'z']
['b', 'c', 'd', 'f', 'g', 'j', 'k', 'l', 'm', 'm', 'n', 'p', 'q', 'q', 'r', 's', 't', 'v', 'v', 'v', 'v', 'w', 'x', 'x', 'x', 'x', 'z', 'z']
['b', 'c', 'd', 'f', 'g', 'j', 'k', 'l', 'm', 'm', 'n', 'p', 'q', 'q', 'r', 's', 'v', 'v', 'v', 'v', 'w', 'x', 'x', 'x', 'x', 'z']
['b', 'c', 'd', 'f', 'g', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'r', 'v', 'v', 'v', 'v', 'w', 'x', 'x', 'x', 'x', 'z']
['b', 'c', 'd', 'f', 'g', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'r', 'v', 'v', 'w', 'x', 'x', 'z']
['c', 'f', 'g', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'r', 'v', 'v', 'w', 'x', 'x', 'z']
['c', 'f', 'g', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'v', 'v', 'w', 'x', 'z']
['c', 'f', 'g', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'v', 'v', 'w', 'z']
['c', 'f', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'v', 'w', 'z']
['c', 'f', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'v']
['c', 'f', 'k', 'n', 'p', 'q', 'q', 'v']
['c', 'n', 'p', 'q', 'q', 'v']
['n', 'p', 'q', 'q']
['p', 'q']
[] <--- Causes the error

Upvotes: 1

Related Questions