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
Post a Comment