-1

I see the point of the question stays in the first elif:

import random as 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] and 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 as you suppose:

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

Any help would be appreciated.

Alex M
  • 2,756
  • 7
  • 29
  • 35
  • According to your question title, your output is correct. None of these 'words' contain characters that appear in the word before it. – Jongware Jul 28 '16 at 09:01
  • Maybe you want to change `if word not in vocabulary:` with `if word not in vocabulary.values():` – alec_djinn Jul 28 '16 at 09:14
  • "cuu" contains one more u than I want. 'vd' contains two consonants. I want only one vowel and one consonants for each pair of two letters. – Gabriele Giordano Jul 28 '16 at 09:41

1 Answers1

0

What you want should be something like this (based on your implementation) :

import random as 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] and len(word)>0:
            if word[-1] not in vowels and alphabet[random_letter] not in consonants:
                word+=alphabet[random_letter]
            elif word[-1] not in consonants and 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 another 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

for i in range(0, 5):
    print(generateWord(2, 5))
Sygmei
  • 467
  • 2
  • 10
  • looks like you used the consonants in the first control and the vowels in the second. In my latest version I used the opposite. But I don't understand why I had a different result – Gabriele Giordano Jul 28 '16 at 13:31
  • I used "if word[-1] not in vowels and alphabet[random_letter] not in vowels:" you used "if word[-1] not in vowels and alphabet[random_letter] not in consonants:" Now I got it – Gabriele Giordano Jul 28 '16 at 13:33