TypeError:'str'对象在原子文本编辑器usind cmd promt中不可调用

时间:2019-05-10 16:59:05

标签: python

我运行了代码,但收到错误'str' object is not callable

import random, string

def generator():
    letter1 = random.choice(string.ascii_lowercase)
    letter2 = random.choice(string.ascii_lowercase)
    letter3 = random.choice(string.ascii_lowercase)
    letter4 = random.choice(string.ascii_lowercase)
    letter5 = random.choice(string.ascii_lowercase)


letter_input_1 = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter:')
letter_input_2 = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter:')
letter_input_3 = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter:')
letter_input_4 = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter:')
letter_input_5 = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter:')

vowels = 'aeiouy'
consonants = 'bcdfghjklmnpqrstvwxz'
letter = string.ascii_lowercase

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(letter)
    else:
        letter1 = letter_input_1 #allowing user to select a specific letter

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(letter)
else:
        letter2 = letter_input_2 #allowing user to select a specific letter

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(letter)
else:
    letter3 = letter_input_3 #allowing user to select a specific letter

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(letter)
else:
    letter4 = letter_input_4 #allowing user to select a specific letter

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(letter)
else:
    letter5 = letter_input_5 #allowing user to select a specific letter
name = 'letter1+letter2+letter3+letter4+letter5'
'return'(name )

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

它应该根据我输入的变量给出随机字母,但出现错误

File "C:\Users\17348\Desktop\py4e\babynames4.py", line 69, in <module>
    'return'(name )
TypeError: 'str' object is not callable

2 个答案:

答案 0 :(得分:0)

您的代码上的问题是缩进是错误的,而事情本来应该是字符串。

特别是以下几行:

name = 'letter1+letter2+letter3+letter4+letter5'
'return'(name )

通过正确地缩进它们并将字符串转换为语句,您将获得以下代码,这些代码似乎可以执行您想要的操作:

import random, string

letter_input_1 = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter:')
letter_input_2 = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter:')
letter_input_3 = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter:')
letter_input_4 = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter:')
letter_input_5 = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter:')

vowels = 'aeiouy'
consonants = 'bcdfghjklmnpqrstvwxz'
letter = string.ascii_lowercase

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(letter)
    else:
        letter1 = letter_input_1 #allowing user to select a specific letter

    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(letter)
    else:
            letter2 = letter_input_2 #allowing user to select a specific letter

    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(letter)
    else:
        letter3 = letter_input_3 #allowing user to select a specific letter

    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(letter)
    else:
        letter4 = letter_input_4 #allowing user to select a specific letter

    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(letter)
    else:
        letter5 = letter_input_5 #allowing user to select a specific letter
    name = letter1+letter2+letter3+letter4+letter5
    return(name )

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

答案 1 :(得分:0)

这是精简版,对于初学者来说仍然很容易阅读:

import random, string

def generator(total):
    vowels = 'aeiouy'
    consonants = 'bcdfghjklmnpqrstvwxz'
    letter = string.ascii_lowercase

    choices = [""] * total
    for choice in choices:
        choice = random.choice(string.ascii_lowercase)

    selections = [""] * total
    for x, selection in enumerate(selections):
        selection = input('choose a letter..."v"for vowels, "c" for consonants, "l" for any other letter, or pick any letter: ')

        if selection == "v":
            choices[x] = random.choice(vowels)
        elif selection == "c":
            choices[x] = random.choice(consonants)
        elif selection == "l":
            choices[x] = random.choice(letter)
        else:
            choices[x] = selection #allowing user to select a specific letter

    return("".join(choices))

for babynames in range(20):
    print(generator(5))