Python sample code for implementing the login interface
I have previously written the Python sample code for implementing the login interface. Recently I need to review it and I will send it to the essay by the way.
Requirements:
1. Enter the user name and password
2. the authentication is successful and the welcome information is displayed.
3. log out of the program after three incorrect user names are entered.
4. After three wrong passwords are entered, the user name will be locked.
Readme:
1.UserList.txt is a file that stores the user name and password. The format is: username: password. Each line stores one user information.
2.LockList.txt is a file that stores the Locked User name. It is empty by default.
3. the user enters the user name. The program first queries the lock list LockList.txt. If the user name is in it, the system prompts the user to be locked and exits the program.
4. If the user name is not in the lock list, the program will query the user name list UserList.txt. If the user name is not in the list, it will prompt that the user does not exist. Please enter it again. If the user name is incorrect three times, the program will exit.
5. If the user name is in the User Name List, the user will be prompted to enter the password, the password is correct, and the welcome information will be displayed; 3 input errors will lock the user name (write to the lock List)
Flowchart:
Code:
# Joe Youngimport OS, sys, getpassos. system ('cls') # call the system method of the OS module to pass in the 'cls' parameter, clear screen count = 0 # username logon count while count <3: username = input ('username: ') lock_file = open('LockList.txt', 'r + ') Open the locklist.txt file with the permission r + (open for reading and writing files. The file pointer is placed at the beginning of the file) lock_list = lock_file.readlines () rows, generate the list, and assign the value to lock_list for lock_line in lock_list: if username = lock_line.strip ('\ n '): the token uses strip(token token to remove the line break, and then the token userNameToken is in locklist.txt print ('username % s has been locked. Please contact the administrator... '% (username) sys. exit (1) # The exit () method of the sys module indicates to exit with open('UserList.txt ', 'R') as user_file: Allow to open userlist.txt, and read-only user_list = user_file.readlines () # Read UserList row by row. tx T file, assigned to the user_list variable for user_line in user_list: (user, passwd) = user_line.strip ('\ n '). split (':') # obtain the value of user and passwd. Use split (':') to split the string if user = username: login Login User name in the userlist.txt file n = 0 # password Input count while n <3: #3 input opportunity password = getpass. getpass ('password: ') # Use the getpass () method of the getpass module to obtain the user-Entered password if password = passwd: # determine if the Password Matches print ('Welcome % s to log on to the system! '% (Username) sys. exit (0) else: if n! = 2: # n = 2, is the last opportunity, do not need to prompt the remaining 0 opportunities print ('wrong password, please enter again, you still have % d chances \ n' % (2-n) n + = 1 # incorrect password, times + 1 else: lock_file.write (username + '\ n'); incorrect password input times up to 3 times, write the user name to the locklist.txt file, lock the user name sys. exit ('too many errors, the user name has been locked... ') # exit the program and output the prompt else: # the user name does not exist. Run the else statement if count! = 2: # count = 2 is the last chance to enter the user name. You do not need to prompt that there are still 0 chances to print ('user name does not exist. Please try again, you still have % d chances \ n' % (2-count) count + = 1 # incorrect user name input, count + 1 else: # The number of user name input errors reaches 3 times sys. exit ('Too many inputs, the program has exited... ') # exit the program and output the following message: lock_file.close () closing the locklist.txt File
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.