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

2024-03-28 15:35:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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

Tags: csvtoinforinputifcheckelse
1条回答
网友
1楼 · 发布于 2024-03-28 15:35:06

当您调用readlines时,它返回一个行列表,对该列表中的一个项调用strip("\n")将返回一个元组,然后所有值都将存储在元组的第一个元素中,但第二个元素为空。您所需要的就是strip(",")在一行中获取所有值。你知道吗

相关问题 更多 >