“IndexError:字符串索引超出范围”错误,尽管代码

时间:2018-04-22 13:04:34

标签: python text-files

我正在为我正在开发的Python测验创建注册模块,仅用于改进我的编程技术。 这是我的代码:

def quiz():
    print('success')
def loginPage():
    fr=open("ddddfile.txt",'r').read()
    v=False
    while v==False:
        un=input('Please enter your username.')
        pw=input('Please enter your password.')
        v2=0
        print(fr[v2][2], fr[v2][3])
        if fr[v2][3]==un and fr[v2][4]==pw:
            print('Successfully logged in.')
            v=True
            quiz()
        else:
            print('Incorrect username or password. Please try again.')
            v2+=1

def registerPage():                                               
    v=0                                                                     
    while v<3:                                                  
        n=input('Please enter your name.')                         
        if n==str(n):                                      
            v+=1                                                   
        a=input('Please enter your age.')                           
        if int(a)>10 and int(a)<20:                               
            v+=1                                                    
        yg=input('Please enter your year group.')        
        if int(yg)>6 and int(yg)<15:                              
            v+=1                                            
    un=n[:3]+a                                               
    print('Your new username is', un, '. Please remember this as you will need it to log in.')
    v=False                                                  
    while v==False:                                         
        pw=input('Please enter your desired password.')       
        pwc=input('Please re-enter your password.')
        if pw==pwc:
            v=True
    usrInf=[n,a,yg,un,pw]
    fw=open("ddddfile.txt",'r+')
    fw.write(',')
    fw.writelines(str(usrInf))
    fw.close()
    loginPage()

def startPage():
    c1=input('Welcome to the Quiz. Please type "login" or "register".')
    v=False
    while v==False:
        if c1.lower()=='login' or c1.lower()=='l':
            v=True
            loginPage()
        elif c1.lower()=='register' or c1.lower()=='r':
            v=True
            registerPage()
        else:
            c1=input('Please type "login" or "register".')
startPage()

如您所见,我将用户数据存储在外部文本文件中。文本文件的内容如下:

[['Xavier Pamment', '16', '11', 'Xav16', 'nL4WVba2KR'],[]]

但是,在 loginPage()函数中,运行时,一旦代码尝试验证用户输入的详细信息,我就会收到此错误:

Traceback (most recent call last):
File "C:\Users\xavier\Desktop\dddd.py", line 58, in <module>
    startPage()
File "C:\Users\xavier\Desktop\dddd.py", line 52, in startPage
    loginPage()
File "C:\Users\xavier\Desktop\dddd.py", line 10, in loginPage
    print(fr[v2][2], fr[v2][3])
IndexError: string index out of range

有谁知道我在哪里滑了? 谢谢, CH

1 个答案:

答案 0 :(得分:1)

执行fr=open("ddddfile.txt",'r').read()时,您将以字符串的形式获取文件的内容。在尝试以您的方式对其进行索引之前,您需要将该字符串转换为列表。你应该研究ast.literal_eval

相关问题