Z.Q Z.Q - 7 days ago 6
Python Question

Name generator verbose code; looking for concise & nice solutions

The following code works if user input is less than 6, but even as a beginner I don't think this is the correct way to do it, is there any concise and pretty solution for this?

code description: For example, user enters

4 v v v v
, this name generator will generate a names with four vowels 20 times.

import random, string

vowels = "aeiou"
consonants = "bcdfghjklmnpqrstvwxz"
letters = string.ascii_lowercase

nameLenInput = int(input("how long do you wanna the name to be? max 6 characters"))
if nameLenInput == 6:
letter_input_1=input("what letter do you want for 1st cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_2=input("what letter do you want for 2nd cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_3=input("what letter do you want for 3rd cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_4=input("what letter do you want for 4th cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_5=input("what letter do you want for 5th cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_6=input("what letter do you want for 6th cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
elif nameLenInput == 5:
letter_input_1 = input(
"what letter do you want for 1st cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_2 = input(
"what letter do you want for 2nd cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_3 = input(
"what letter do you want for 3rd cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_4 = input(
"what letter do you want for 4th cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_5 = input(
"what letter do you want for 5th cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")

elif nameLenInput == 4:
letter_input_1 = input(
"what letter do you want for 1st cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_2 = input(
"what letter do you want for 2nd cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_3 = input(
"what letter do you want for 3rd cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_4 = input(
"what letter do you want for 4th cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")

elif nameLenInput == 3:
letter_input_1 = input(
"what letter do you want for 1st cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_2 = input(
"what letter do you want for 2nd cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_3 = input(
"what letter do you want for 3rd cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")

elif nameLenInput == 2:
letter_input_1 = input(
"what letter do you want for 1st cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")
letter_input_2 = input(
"what letter do you want for 2nd cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")

elif nameLenInput == 1:
letter_input_1 = input(
"what letter do you want for 1st cha? Enter 'v' for Vowel, 'c' for consonants, 'l' for any letter: ")

def generator():
if letter_input_1=="v":
letter1=random.choice(vowels)

elif letter_input_1=="c":
letter1=random.choice(consonants)

elif letter_input_1=="l":
letter1=random.choice(letters)

else:
letter1=letter_input_1
if nameLenInput == 1:
name = letter1
return name

if letter_input_2=="v":
letter2=random.choice(vowels)

elif letter_input_2=="c":
letter2=random.choice(consonants)

elif letter_input_2=="l":
letter2=random.choice(letters)

else:
letter2=letter_input_2

if nameLenInput == 2:
name = letter1+letter2
return name

if letter_input_3=="v":
letter3=random.choice(vowels)

elif letter_input_3=="c":
letter3=random.choice(consonants)

elif letter_input_3=="l":
letter3=random.choice(letters)

else:
letter3=letter_input_3

if nameLenInput == 3:
name = letter1 + letter2 + letter3
return name


if letter_input_4 == "v":
letter4 = random.choice(vowels)

elif letter_input_4 == "c":
letter4 = random.choice(consonants)

elif letter_input_4 == "l":
letter4 = random.choice(letters)

else:
letter4 = letter_input_4

if nameLenInput == 4:
name = letter1 + letter2 + letter3 + letter4
return name

if letter_input_5 == "v":
letter5 = random.choice(vowels)

elif letter_input_5 == "c":
letter5 = random.choice(consonants)

elif letter_input_5 == "l":
letter5 = random.choice(letters)

else:
letter5 = letter_input_5

if nameLenInput == 5:
name = letter1 + letter2 + letter3 + letter4 + letter5
return name

if letter_input_6 == "v":
letter6 = random.choice(vowels)

elif letter_input_6 == "c":
letter6 = random.choice(consonants)

elif letter_input_6 == "l":
letter6 = random.choice(letters)

else:
letter6 = letter_input_6

name=letter1+letter2+letter3+letter4
return name

for i in range(20):
print(generator())


Does the solution to make this code concise and smart has anything to do with algorithm?

Answer

Since you asked for concise, you could do something like this:

import random

vowels = "aeiou"
consonants = "bcdfghjklmnpqrstvwxz"
letters = vowels + consonants

#dictionary to associate letter codes with letter sets:
d = {'v':vowels, 'c':consonants, 'l':letters}

s = input("""Enter a string such as "vcllvc" which indicates
where you would like vowels, consonants,
and unrestricted letters to be: """)

for i in range(20):
    name = ''.join(random.choice(d[t])for t in s)
    print(name)

Typical run:

Enter a string such as "vcllvc" which indicates
where you would like vowels, consonants,
and unrestricted letters to be: clvccvl
nfusjoo
tzofkuf
vnihtut
vqarvoi
hyisxaj
hsaxcef
daobdai
byewkar
vlaznic
mbomsuw
mauhkic
rhumbea
fticmuz
ciacfof
zgezdak
mdahwed
jcavtam
tnofmeu
baowkeg
miedpuh                                                 
Comments