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)
Aucun commentaire:
Enregistrer un commentaire