Python Learning 1: User Logon, python learning user logon
User Login requirements:
1. the user name and password must be saved in the file.
2. You can log on three times. After three times, the account is locked. The user lock information is saved in the file. The user enters the correct reset lock information.
Program code:
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
Username = [] # -----------------------------------------------
For line in open('user.txt '):
Line = line. replace ('\ n', ''). split () extract the username from the user.txt file and save it to the username list.
Username. append (line [0]) # -----------------------------------------------
Password = [] # -----------------------------------------------
For line in open('user.txt '):
Line = line. replace ('\ n', ''). split () keystore to retrieve the password from the user.txt file and save it to the List password.
Password. append (line [1]) # -----------------------------------------------
Counter = [] # -----------------------------------------------
For line in open('user.txt '):
Line = line. replace ('\ n', ''). split () Fetch the user account status from the user.txt file and save it to the counter list.
Counter. append (line [2]) # -----------------------------------------------
New_counter = [] # -----------------------------------------------
For n in counter:
New_counter.append (int (n) # convert the elements in counter to numeric characters
Counter = new_counter #-----------------------------------------------
Flag = True
While flag:
If min (counter) <3: # determine the status of all users. As long as a user can log on, the program can run.
Usname = input ("Enter the User name :")
If usname in username: # if the user name is in the user list
P = username. index (usname) # determine the location of the input username in the list
While True:
If counter [p] <3: # determine whether the selected user is locked
Psword = input ("enter the password :")
If psword = password [p]:
Print ("Congratulations! Username. The password is entered correctly! ")
Counter [p] = 0 #-----------------------------------------------
Newuser = open ("user.txt", 'w ')
For I in range (len (username): # The user password is entered correctly, the user status is reset, and the user file is written.
Newuser. write ('% s % d \ n' % (username [I], password [I], counter [I])
Newuser. close ()#-----------------------------------------------
Flag = False # set the value of the outermost loop to False to exit the final loop.
Break # exit the current loop
Else:
Print ("Incorrect username and password ......")
Counter [p] + = 1 # counter auto-increment 1
Newuser = open ("user.txt", 'w ')#-----------------------------------------------
For I in range (len (username): # Write the user error count to the User File
Newuser. write ('% s % d \ n' % (username [I], password [I], counter [I])
Newuser. close ()#-----------------------------------------------
Break
Else:
Print ("This user is locked...") # When the user status is locked, the system prompts the user
Break
Else:
Print ("This user is not available, please enter again") # The user name is incorrect and prompt the user to enter again
Else:
Print ("all users are locked, and the program cannot be opened...") # determine that all users are locked and the program exits
Break
User Files:
Songyang sy123 0 # The first column is the user name, the second column is the password, and the third column is the user status
Admin ad123 0
Important Position of the program:
1,
Username = [] # create an empty list named username
For line in open('user.txt '): displays each row of the user.txt file.
Line = line. replace ('\ n', ''). split () stores all values of each row in the user.txt file in the line list. replace () function
# Replace line breaks at the end of each line with spaces. The split () function uses spaces as the marker to split each line of the file into a list.
Username. append (line [0]) # Save the first element in the line list to the username list to form a user name list
2,
Newuser = open ("user.txt", 'w') # open the User File in write mode. The content in the file will be read into the memory, and the program will delete the file content.
For I in range (len (username): # The program detects several elements in the username list and loops several times.
Newuser. write ('% s % d \ n' % (username [I], password [I], counter [I]) # convert all the list username, password, counter content is written to the file
Newuser. close () # close the file
Program running error description:
This is the corresponding choice. If ":" is not added after statements such as loop
This is because the corresponding error line is not indented.