如何解决IndexError:列表索引超出范围

时间:2019-03-20 13:01:06

标签: python python-2.7 text-files

道歉,因为我是编程界新来的新手。

当我第一次尝试运行以下代码时,它可以正常运行,我可以完美地执行所有3个操作,但是当我再次尝试运行相同的代码时,出现“列表索引超出范围”错误”。

任何帮助和建议将不胜感激。

# To create a new file

ob=open(r'C:\Python27\avi_file_handling\cred_assgn.txt','w')
ob.write("Number" + "-" + "Username" + ":" + "Password" + '\n')
i=0
for x in range(5):
    uid=raw_input("Please enter your username to login\n")
    pswd=raw_input("Please enter your password {} to login\n".format(uid))
    ob.write(str(i) + "     " "-" " " + uid + " : " + pswd + '\n')
    i=i+1   
ob.close()

根据上面的代码创建新文件后,我运行下面的代码来调用这些操作。

import re

# function to validate the userId and password for the customers present in a text file
def validate_user():  
    T=0
    global uid,pswd,T,c1,f1
    f1=open(r'C:\Python27\avi_file_handling\cred_assgn.txt','r+')
    f1.seek(0)
    print f1.tell()
    uid=raw_input("Please enter your username to validate\n")
    print f1.tell()
    pswd=raw_input("Please enter your password {} to validate\n".format(uid))
    print f1.tell()
    for line in f1:

        c1=re.split(r'(-|:+|)',line.replace(' ','').strip())
        if((uid==c1[2]) and (pswd==c1[4])):
            print("welcome sir ",uid,"you are a valid user\n")
            print f1.tell()
            T+=1
            return

    else:
        print("Sir",uid, "Please enter the correct Username and Password")

#Function to add a new user            
def AddNew_User():
    with open(r'C:\Python27\avi_file_handling\cred_assgn.txt','r+') as ob1:
        uid=raw_input("Please enter username to Register\n")
        ob1.seek(0,0)
        z=0
        print ob1.tell()
        for line in ob1:
            z=z+1
            x=z

            #z=sum(1 for _ in ob1)
            c1=re.split(r'(-|:+|)',line.replace(' ','').strip()) 

            if uid==c1[2]:    
                print "Sorry sir {},you are already registered, we cannot register you again".format(uid)
                break
        else:
            pswd=raw_input("Please enter your password for userid {} to register\n".format(uid))
            print ob1.tell()
            if uid==pswd:
                print "Sorry {} sir you cannot have same username and password".format(uid)
                return
            pswd2=raw_input("Please confirm your password once again for userid {} to register\n".format(uid))
            print ob1.tell()
            if pswd!=pswd2:
                print "You have entered incorrect confrim password"
                return

            print ob1.tell()
            print  "Hello {} you have successful registered to us".format(uid)
            print ob1.tell()
            ob1.write(str(x) + "     " "-" " " + uid + " : " + pswd + '\n')
            print ob1.tell()

            ob1.close()

#Function to update the password of an Existing user
def Update_password():

    #print "up" ,f1.tell()
    #global f1

    validate_user()
    #print "up" ,f1.tell()
    print "previous one",c1

    if T>0:

        #print "up" ,f1.tell()
        pswd3=raw_input("Hello {} Please entrer your new password")
        #print "-up" ,f1.tell()
        pswd4=raw_input("Hello {} once agin password")
        print "1--up" ,f1.tell()
        print pswd,pswd4,pswd3,uid
        if  pswd==pswd3:
            print "Please enter the new password and do not repeat"
        elif pswd3==pswd4:
            #print "---up" ,f1.tell()
            print c1[4]
        print "11--up" ,f1.tell()
        f1.seek(0)
        ch=f1.read().replace(c1[4],pswd4)
        print "2----up" ,f1.tell()
        print ch
        print "3----wup" ,f1.tell()
        #ob.seek(2)
        print "4----qup" ,f1.tell()
        f1.seek(2)
        f1.write(ch)
        print "5----qup" ,f1.tell()
        #contents = f.read().replace('Total: __00__', total)
        #print "up" ,f1.tell()
        print "poassower matcg"
        #ob.seek(0,0)    
        f1.close()

#Main function calling  
while(True):
    Inputs=input("Please select the operation you would like to perform sir\n 1) Valudate user\n 2) Add new user \n 3) Update password \n5)Exit from the program")
    if Inputs==1:
        validate_user()
    elif Inputs==2:
        AddNew_User()
    elif Inputs==3:
        Update_password()
    elif Inputs==5:   
        break

0 个答案:

没有答案