列表索引超出范围索引错误?

时间:2018-06-30 07:52:41

标签: python list index-error

我是python的新手,我正在写我最大的项目之一。但是,尝试写入csv文件时遇到错误。有人可以看到我的代码有什么问题吗?一直在尝试查找错误已有一段时间。

a = open("accounts1.csv", "r")
contents1 = a.readlines()
newContents1 = []
for row1 in contents1:
    rowStrip = row1.strip("\n")
    columns1 = rowStrip.split(",")
    newContents1.append(columns1)
b = open("accounts1.csv", "a")
def mainMenu():
    menu1 = input("1 - Enter a film you have recently watched: \n"
                          "2 - Recieve Recomendations o n Films: \n"
                          "3 - Like a Film: \n"
                          "4 - Exit")
    if menu1 == "1":
        films = []
        amount = int(input("How many films have you watched recently: "))
        for i in range(amount):
            film = input("Enter the film name: ")
            films.append(film)
        films = ", ".join(films)
        b.write(films)
menu = input("Enter 1 to login \n"
         "Enter 2 to create an account \n"
         "Enter 3 to exit")
if menu == "1":
    check = True
    while check == True:
        uname = input("Enter your Username (Case Sensitive)")
        pword = input("Enter your Password (Case Sensitive)")
        for lines in newContents1:
            if lines[1] == uname and lines[0] == pword:
                print("You're In")
                check = False
                mainMenu()
            else:
                if lines > columns1:
                    print("Incorrect Password or Username")
                    again = input("Would you like to try again? (Yes or No)")
                    if again.lower() == "yes":
                        check = True
                    elif again.lower() == "no":
                        print("Goodbye")
                        check = False
                    else:
                        pass
                    break
                else:
                    pass
        else:
            pass

1 个答案:

答案 0 :(得分:0)

当您调用readlines时,它返回一个行列表,在该列表的某个项目上调用strip("\n")将返回一个元组,然后所有值将存储在该元组的第一个元素中,但是第二个是空的。您只需要strip(",")就能将所有值放在一行中。