Python 名称生成器详细代码;寻找简明的;不错的解决方案

Python 名称生成器详细代码;寻找简明的;不错的解决方案,python,algorithm,input,Python,Algorithm,Input,如果用户输入少于6,下面的代码可以工作,但即使是初学者,我也不认为这是正确的方法,有没有简洁而漂亮的解决方案 代码说明:例如,用户输入4v,此名称生成器将生成包含四个元音的名称20次 import random, string vowels = "aeiou" consonants = "bcdfghjklmnpqrstvwxz" letters = string.ascii_lowercase nameLenInput = int(input("how long do you wanna

如果用户输入少于6,下面的代码可以工作,但即使是初学者,我也不认为这是正确的方法,有没有简洁而漂亮的解决方案

代码说明:例如,用户输入
4v
,此名称生成器将生成包含四个元音的名称20次

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())
使代码简洁和智能化的解决方案是否与算法有关

import random, string

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

nameLenInput = int(input("Enter the length of name: "))

selectedLetters = []
for x in range(nameLenInput):
    l = input("What letter you want for % cha? 'v' for vowel, 'c' for consonant, 'l' for any letter: "%(x+1))
    selectedLetters.append(l)

def generate():
    generated = ''

    for letter in selectedLetters:
        if letter=='v':
            generated+=random.choice(vowels)
        elif letter=='c':
            generated+=random.choice(consonants)
        elif letter=='l':
            generated+=random.choice(letters)
    return generated


for i in range(20):
    print(generate())
我希望这有帮助


我希望这能有所帮助。

既然你要求简明,你可以这样做:

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)
典型运行:

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                                                 

既然你要求简洁,你可以这样做:

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)
典型运行:

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                                                 


for
循环似乎是最明显的。为什么不让用户输入一个字符串,比如
“cvvlvc”
,然后简单地从输入中构造那个随机名称呢?无需逐字纠缠用户。请在整个问题上停止使用粗体。您能更具体一点吗?像完整的代码那样工作?我非常擅长处理代码xD。对于像是家庭作业的东西,我不太愿意给出完整的代码。
for
循环似乎是最明显的。为什么不让用户输入一个字符串,比如
“cvvlvc”
,然后简单地从输入中构造那个随机名称呢?无需逐字纠缠用户。请在整个问题上停止使用粗体。您能更具体一点吗?像完整的代码那样工作?我非常擅长处理代码xD。我不太愿意为一些看起来像是家庭作业的东西提供完整的代码。难以置信,我不可能期望有9行代码,仍在处理中……如何在单独的行中完成这行代码
name='''.join(random.choice(d[t])表示s中的t)
不知道我把
''放在哪里。join
在单独的行中,控制台总是一次打印一个字母,@Z.Q不太确定你在问什么
'.join()
中包含的任何iterable中的字符串连接在一起。作为提示,表达式
''.join(s中的t的random.choice(d[t])
在功能上等同于
'.join(listOfChars)
,其中
listOfChars=[random.choice(d[t])对于s中的t
(尽管前者的内存效率高于后者)。阅读列表的理解,这样您就可以生成一个包含所需字符的列表,然后加入该列表。一旦你习惯了这种两行的方法,就跳过第一步创建列表的部分,只使用一个连接。我知道这一行的作用是什么
name=''。连接(random.choice(d[t])对于t in s)
,我学会了列表理解,但它有点太简洁了,我不知道如何在单独的行中进行,我写了一个列表,但它只对单字符输入有效,当我尝试输入像vv这样的东西时,它给了我这个键错误“vv”,难以置信,我不可能期望有9行代码,仍在处理……你如何在单独的行中处理这行代码
name='''.join(random.choice(d[t])表示s中的t)
不知道我把
''放在哪里。join
在单独的行中,控制台总是一次打印一个字母,@Z.Q不太确定你在问什么
'.join()
中包含的任何iterable中的字符串连接在一起。作为提示,表达式
''.join(s中的t的random.choice(d[t])
在功能上等同于
'.join(listOfChars)
,其中
listOfChars=[random.choice(d[t])对于s中的t
(尽管前者的内存效率高于后者)。阅读列表的理解,这样您就可以生成一个包含所需字符的列表,然后加入该列表。一旦你习惯了这种两行的方法,就跳过第一步创建列表的部分,只使用一个连接。我知道这一行的作用是什么
name=''。连接(random.choice(d[t])对于t in s)
,我学会了列表理解,但它有点太简洁了,我不知道如何在单独的行中进行,我创建了一个列表,但它只对单字符输入有效,当我尝试放置类似vv的东西时,它给了我这个键错误“vv”,它有一个错误,表示未定义原始输入…我只使用过Python3,这是Python2吗?它有一个错误,表示未定义原始输入…我只使用过Python3,这是Python2吗?