python - How can I create a word that does not contain the previous letter contained in the word? -


i see point of question stays in first elif:

import random rnd  vowels="aeiou" consonants="bcdfghlmnpqrstvz" alphabet=vowels+consonants  vocabulary={} index=0 word="" positions=[] while index<5:     random_lenght=rnd.randint(2,5)     while len(word)<random_lenght:         random_letter=rnd.randint(0,len(alphabet)-1)         if len(word)==0:             word+=alphabet[random_letter]         elif random_letter != positions[-1] , len(word)>0:             if word[-1] not in vowels:                 word+=alphabet[random_letter]             if word[-1] not in consonants:                 word+=alphabet[random_letter]         elif random_letter == positions[-1]:             break                   if random_letter not in positions:            positions.append(random_letter)     if word not in vocabulary:         vocabulary[index]=word         index+=1     word="" 

the result doesn't satisfy me suppose:

{0: 'in', 1: 'th', 2: 'cuu', 3: 'th', 4: 'vd'} 

any appreciated.

what want should (based on implementation) :

import random rnd  vowels="aeiou" consonants="bcdfghlmnpqrstvz" alphabet=vowels+consonants  vocabulary={} index=0 word="" positions=[] while index<5:     random_lenght=rnd.randint(2,5)     while len(word)<random_lenght:         random_letter=rnd.randint(0,len(alphabet)-1)         if len(word) == 0:             word+=alphabet[random_letter]         elif random_letter != positions[-1] , len(word)>0:             if word[-1] not in vowels , alphabet[random_letter] not in consonants:                 word+=alphabet[random_letter]             elif word[-1] not in consonants , alphabet[random_letter] not in vowels:                 word+=alphabet[random_letter]         if random_letter not in positions:            positions.append(random_letter)     if word not in vocabulary:         vocabulary[index]=word         index+=1     word="" 

and version :

import string import random  isvowel = lambda letter: letter in "aeiou"  def generateword(lengthmin, lengthmax):     word = ""     wordlength = random.randint(lengthmin, lengthmax)     while len(word) != wordlength:         letter = string.ascii_lowercase[random.randint(0,25)]         if len(word) == 0 or isvowel(word[-1]) != isvowel(letter):             word = word + letter     return word  in range(0, 5):     print(generateword(2, 5)) 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -