loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import string
import random

vowels = 'aeiou'
consonants = 'bcdfghijklmnpqrstvwxz'


def get_letter(last_letter=None):
    # Check if last letter generated was a consonant
    if last_letter:
        last_letter = ''.join(last_letter)
        last_letter_was_consonant = last_letter in consonants
    else:
        last_letter_was_consonant = False

    # If last letter was a vowel, make it 50% chance to generate a consonant
    if not last_letter_was_consonant and bool(random.randint(0, 1)):
        last_was_consonant = True
        yield random.choice(consonants)
    else:
        last_was_consonant = False
        yield random.choice(vowels)


def generate_word(length, spread=0):
    word = list()

    for i in range(length + random.randint(1, spread)):
        word.append(next(get_letter(last_letter=word[-1:])))

    return ''.join(word).capitalize()


if __name__ == '__main__':
    print('Generating 10 random names:')
    for i in range(10):
        print(generate_word(length=5, spread=3))