0

I am trying to create a program on python to do with manipulating lists/arrays. I am having trouble with an error:

lowercase = names.lower
AttributeError: 'list' object has no attribute 'lower'

I really need some help to fix this!

names = [] #Declares an array
print("Type menu(), to begin")
def menu():

    print("----------------------------MENU-----------------------------")
    print("Type: main() for core functions")
    print("Type: delete() to delete a name")
    print("Type: save() to save the code")
    print("Type: load() to load the saved array")
    print("Type: lower() to make all items in the list lower case")
    print("-------------------------------------------------------------")

def main():
    times = int(input("How many names do you want in the array? ")) #Asks the user how many names they want in the array
for i in range(times):
    names.append(input("Enter a name ")) #Creates a for loop that runs for the amount of times the user requested, it asks the user to enter the names
choice = input("Would you like the array printed backwards? ") #asks the user whether they want the array backwards
if choice == "Yes":
    names.reverse() #If the user says yes, the array is reversed then printed backwards
    print(names)
else:
    print(names) #Otherwise, the array is printed normally
number = int(input("Which item would you like to print out? "))
number = number - 1
print(names[number])
start = int(input("What is the first position of the range of items to print out? "))
start = start - 1
end = int(input("What is the last position of the range of items to print out? "))
print(names[start:end])

def delete():
    takeAway = input("Which name would you like to remove? ")
    names.remove(takeAway)
    print(names)

def save():
    saving1 = open("Save.txt", 'w')
    ifsave = input("Would you like to save the array? ")
    if ifsave == "Yes":
        for name in names:
                saving1.write("%s\n" % name)
                saving1.close
    else:
        menu()
def load():
    loadquestion = input("Would you like to load a list of names? ")
    if loadquestion == "Yes":
        saving1 = open('Save.txt', 'r')
        print(saving1.read())
        saving1.close()
    else:
        menu()
def lower():
    lowerq = input("Would you like to make the array lowercase? ")
    if lowerq == "Yes":
        lowercase = names.lower
        print(lowercase)
    else:
        menu()
1

2 Answers 2

3

The variable names is a list. You can't use the .lower() method on a list.

pp_ provided the solution:

lowercase = [x.lower() for x in names]

While not exactly equivalent to the previous example, this may read better to you and has, effectively, the same result:

lowercase=[]
for name in names:
    lowercase.append(name.lower())

Alternate solution that may fit your needs:

print (str(names).lower())
Sign up to request clarification or add additional context in comments.

Comments

2

Like the error message says, you can't use .lower() on lists, only on strings. That means you'll have to iterate over the list and use .lower() on every list item:

lowercase = [x.lower() for x in names]

2 Comments

Another thing to note is that he wasn't actually calling .lower anyway; he was just mentioning it. A second way to do it would be to use map(): lowercase = map(str.lower, names).
also, since this user is obviously new to python, it might be helpful to write out what this list comp actually does: lowercase = [] for x in names: # iterates over list of names lowercase.append(x.lower())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.