Python write ' User login ' procedure 2

Source: Internet
Author: User

Last written the login program is disorganized, I can not see, the teacher made the comments as follows

Since the current dictionary is not too familiar with the first time,

And then joins in a while loop really works

But the sense code is still more redundant, the readability is super poor

Yes, now find a more useful debugging method, is to add a piece of print statement when each function is completed to show that the code is running, or how to implement a function

The last time the code has a super bug is that when a user name is locked, when you enter the correct password again, will still log on,

And I corrected it this time, and here's the code.

User_name=[' A ', ' B ', ' C ', ' d '] #密码和字符串分别保存到两个列表内并对应位置passward =[' 1 ', ' 2 ', ' 3 ', ' 4 ']error_record=[]while (1): Error_record = [] Input_user_name=input ("Please enter user name") #将格式转化为字符串格式 input_passward=input ("Enter password") if Input_user_n Ame in user_name: # To determine if the user name exists user_name_position = User_name.index (input_user_name) # Gets the user name in the list position with OP En (' error_record.txt ') as F: # Open the file and return the object for lines in F: # Loop The data for each line rs = line.replace (' \ n ', ' ') # Replace line break Error_record.append (RS) # Loop put in the list f.close () print (Error_record) print ("User            exist, and write the list Error_record ") if Input_user_name in Error_record: # If the list has this user name print (" user exists in TXT record ") User_name_position1 = Error_record.index (input_user_name) # Find the location of this username # if Error_record[user_name_position1 + 1] = = ' 3 ': # Match username location to determine if the password is correct # print ("Login failed, account is locked") # elif Input_passward = = Passward[user_n Ame_position]: # corresponding User fameTo determine if the password is correct # # print ("Login successful") if Error_record[user_name_position1 + 1] = = ' 1 ':                    if Input_passward = = Passward[user_name_position]: print ("login Successful") Else: Error_record[user_name_position1 + 1] = ' 2 ' # print ("Login failed, there are 1 chances") print (E                    Rror_record) with open (' Error_record.txt ', ' W ') as F:f.truncate () # Document empty                            For i in Error_record: # loop writes a new list of documents with open (' Error_record.txt ', ' a ') as F:                    F.write (i) f.write (' \ n ') F.close () Print ("Write TXT record 2") Elif Error_record[user_name_position1 + 1] = = ' 2 ': if Input_passward = =                    Passward[user_name_position]: # corresponds to the user name location to determine if the password is correct print ("login Successful") Else:    Print ("Login failed with 0 chances")                Error_record[user_name_position1 + 1] = ' 3 ' # print (Error_record) W ITH open (' Error_record.txt ', ' W ') as F:f.truncate () # Document empty for I in Error_reco                            RD: # Loop writes a new list of documents with open (' Error_record.txt ', ' a ') as F:f.write (i)            F.write (' \ n ') F.close () print ("Write to TXT record 3") Elif Error_record[user_name_position1 + 1] = = ' 3 ': Print ("Login failed, account locked") else:if input                _passward = = Passward[user_name_position]: # corresponds to the user name location to determine if the password is correct print ("login Successful") Else: Error_record=[] with open (' Error_record.txt ') as F: # Open the file and return the object for lines in F: # on each line Data Loop rs = line.replace (' \ n ', ') # Replace line break Error_record.append (RS) # Loop drop to column In-table F.closE () print ("Put some data in the text in the list") print (Error_record) error_record.append (input_user _name) error_record.append (' 1 ') print ("No Error record for this user, added to Error_record list") print                (Error_record) with open (' Error_record.txt ', ' W ') as F:f.truncate () # Document empty                        For i in Error_record: # loop writes a new list of documents with open (' Error_record.txt ', ' a ') as F: F.write (i) f.write (' \ n ') print ("Bad password, two chance") E Lse:print ("Please enter the correct user name")

The code is still super rotten, and I know it, and I'll change it, okay?

Python write ' User login ' procedure 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.