2016-03-09 79 views
0

我想在python上創建一個程序來處理列表/數組。我遇到了一個錯誤:Python列表:.lower()屬性錯誤

lowercase = names.lower
AttributeError: 'list' object has no attribute '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() 
+0

請學會提供[mcve],而不是在此處傾銷所有代碼。 –

回答

2

像錯誤消息說,您不能在列表使用.lower(),只對字符串。這意味着你必須遍歷列表,並在每個列表項使用.lower()

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

另一件需要注意的事情是,他實際上並不*調用*'.lower';他只是提到它。第二種方法是使用'map()':'lowercase = map(str.lower,names)'。 – zondo

+1

也是,因爲這個用戶顯然是python的新手,所以寫出這個列表comp實際上做的事情可能會有幫助: 'lowercase = [] for x in name:#遍歷名字列表 lowercase.append(x .lower())' – n1c9

3

變量names是一個列表。您不能在列表上使用.lower()方法。

PP_提供的解決方案:

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

雖然不是正是相當於前面的例子,這可能讀更好的你,有,有效,同樣的結果:

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

替代解決方案,可能適合您的需求:

print (str(names).lower())