Python's process of writing ' User login program '

Source: Internet
Author: User
Tags string format

Job Requirements:

Basic requirements: Let the user enter the user name password Authentication successful display welcome information after the error three to exit the program upgrade requirements: can support multiple users login (prompt, through the list to save multiple account information) Users 3 times authentication failed, quit the program, restart the program when you try to log on, or lock the status (prompt: Need to save user-locked state to file)

First of all see this demand feeling not too difficult, immediately in the brain over the side of the train of thought, as follows:
1, save the user name and password with two lists, and ensure that the corresponding relationship is one by one to verify the correct password
2, using the Find location function in the list to find the user entered the user name in the list position,
4. If the password is incorrect, write the user name to the file and enter the error number in the next line
5, each login to verify that the file has entered the wrong password of the user name and the number of verification errors reached 3 times

But when it comes to writing, it's not as easy as it might seem, and it's a lot more problematic.

First write out the basic login program

user_name=['a','b','C','D']#passwords and strings are saved in two lists and corresponding locationspassward=['1','2','3','4']input_user_name=STR (Input ("Please enter user name"))#Convert format to string formatINPUT_PASSWARD=STR (Input ("Please enter your password"))ifInput_user_nameinchUSER_NAME:#determine if the user name existsUser_name_position = User_name.index (input_user_name)#get the location of the user name in the list    ifInput_passward = = Passward[user_name_position]:#Determine if the password is correct for the location of the user name        Print("Landing Success")

Start to encounter the first problem, do not know the basic use of the list, and then go to Baidu is now User_name.index (list) This method wasted a lot of time

This is a checked list. How to use a connection

Http://www.cnblogs.com/Devilf/p/8039805.html

Then the second problem is that the input username and password are converted to a string format in order to better find its location in the list, otherwise the input number is not found in the list by default int type

PS: What to consider when the user does not exist

Then you have to retrieve the file for the error record of this account, there is no new add
It should be written later when the user enters the wrong password
Then you have to retrieve the file for the error record of this account, there is no new add

Some words write the number of errors to a list and then write to the document
   I thought I'd write it, it's nothing big, 
and then it's like the last time. How to write a document online now

and of course, ignore it, each time you read the write list from the file, modify the list, and then write back to the document
python read and write files in
77389258


Then I can not write down, out of the wrong too much, or go back to good repair my base it, the thought of a moment will be able to finish, who know yesterday wrote the early morning near two,

Yes, and when you do not know how to name the variable, Baidu translation is a very good choice
and the next time you write a program must think clearly, to head to the end, or you do not know what the problem, it is necessary to first draw out the flowchart
finally attached to my Super Rotten code bar,
# input_user_name= ' A ' user_name=[' a ', ' B ', ' C ', ' d '] #密码和字符串分别保存到两个列表内并对应位置passward =[' 1 ', ' 2 ', ' 3 ', ' 4 ']erro R_record=[]def judge (): Error_record = [] input_user_name=str (Input ("Enter user name") #将格式转化为字符串格式 Input_passward=str ( Input ("Enter password")) if input_user_name in user_name: # To determine if the user name exists user_name_position = User_name.index (input_user_na        Me) # Get the user name in the list if Input_passward = = passward[user_name_position]: # corresponding to the username location determine if the password is correct print ("Login Successful")                    Else:with open (' Error_record.txt ') as F: # Opens the file and returns the object for lines in F: # Loop through each row of data rs = line.replace (' \ n ', ') # Replace line break Error_record.append (RS) # Loop in the list F.clos E () print (Error_record) if input_user_name in Error_record: # If the user name is in the list user_name_ Position1 = Error_record.index (input_user_name) # Find the location of this user name print (user_name_position1) if Error_record[user_name_pOsition1 + 1] = = ' 1 ': # To determine the number of input errors if Input_passward = = passward[user_name_position]: # corresponding to the user name location to determine if the password is correct Print ("Login successful") judge () Else:error _record[user_name_position1 + 1] = ' 2 ' # print (Error_record) with open (' Err  Or_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 () judge () Elif Error_record[user_name_position1 + 1] = = ' 2 ': if Input_passward = = Passward[user_name_po                        Sition]: # corresponds to the user name location to determine if the password is correct print ("Login successful") judge () Else: ErRor_record[user_name_position1 + 1] = ' 3 ' # 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 ') F.close () judge ()                    Elif Error_record[user_name_position1 + 1] = = ' 3 ': Print ("Login failed, account locked")                Judge () Else:error_record.append (input_user_name) error_record.append (' 1 ')  Print (Error_record) with open (' Error_record.txt ', ' W ') as F:f.truncate () #                        Document emptying for I in Error_record: # Loops Write a new list of documents with open (' Error_record.txt ', ' a ') as F: F.writE (i) f.write (' \ n ') F.close () judge () judge () 

  

And then the flowchart will be mended later.


Python's process of writing ' User login program '

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.