Python列表:.lower()属性错误

时间:2016-03-09 18:18:27

标签: python list

我正在尝试在python上创建一个程序来处理列表/数组。我遇到了错误:

  

lowercase = names.lower
  AttributeError:' list'对象没有属性' lower'

我真的需要一些帮助来解决这个问题!

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()

2 个答案:

答案 0 :(得分:3)

变量names是一个列表。您无法在列表中使用.lower()方法。

pp_提供了解决方案:

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

虽然 完全 与上一个示例相同,但这可能会更好地为您所知,并且实际上具有相同的结果:

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

可能符合您需求的替代解决方案:

print (str(names).lower())

答案 1 :(得分:2)

与错误消息一样,您不能在列表中使用.lower(),只能在字符串上使用.lower()。这意味着您必须遍历列表并在每个列表项上使用lowercase = [x.lower() for x in names]

    cameraManager.capturePictureWithCompletition({ (image, error) -> Void in
    self.myImage = image             
})