[Python] Why is there an error when I use the capitalize function? [closed]

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    Why does it output

    AttributeError: 'list' object has no attribute 'capitalize'


    on sentence = names.capitalize()?

    def random_name(amount, name_length=2):
    names = [] # List to store generated names
    consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"]
    vowels = ["a", "e", "e", "i", "o", "u"]

    for _ in range(amount):
    name = "" # Initialize an empty string for each name
    for _ in range(name_length + random.randint(round((name_length - ((name_length * 2) - 1)) / 2), round((name_length - 1) / 2))):
    consonant = random.choice(consonants)
    vowel = random.choice(vowels)
    name += consonant + vowel # Append consonant and vowel to the name
    names.append(name) # Add the generated name to the list
    sentence = names.capitalize()
    print(" ".join(sentence), end=".") # Print all generated names, joined by a comma

    Continue reading...

Compartilhe esta Página