python implements a simple login system
- 2020-12-19 21:08:11
- OfStack
Novice, although relatively simple things, but made me for a long time.
Many imperfections, such as the lock user, the same user entered the wrong password three times will be locked, but if in the second and third user entered the original user, the count will be recalculated.
In addition does not exist the user input password error too many times will also be locked, and then can create a locked user, this will not be done, as long as the detection 1 whether there is a user name can be.
The solution is to add a special count variable in a special file and discard the file blocklist, but I don't know how to operate it, so I will keep it.
Although python does not have main functions like C1 as the program entry, let's make one, easy to understand
''' Log in the system
Requirements:
1 You can choose to create a new user
2 , can log in the original user
3 Enter the wrong password 3 Then the user is locked '''
flag=True # Loop control character
def createuser():
f=open('userlist.txt','r')# Open the file for an existing user, assuming that the file already exists
flag=True
name=f.readlines()
f.close()
while flag:
username=input('username:')
flag2=False# A marker that already exists for the user name
for line in name:
if(username==line.split('*')[0]):
flag2=True
print(" The user name already exists, please re-enter it ")
if flag2!=True:
f=open('userlist.txt','a')# Create a new user
f.write('\n'+username)
f.write('*')
password=input('password:')
f.write(password)
f.close()
break
main()
def login():# Login function, enter password error 3 Then lock the user
count=0# Password error count ,3 Time is locked
f=open('userlist.txt','r')
info=f.readlines()
f.close()
user=None# Repeat the user token
while flag:
flag2=False
f2=open('blocklist.txt', 'r')
block_name=f2.readlines()
f2.close()
username=input('username:')
if user==None:#user If it is not used, the user name entered is given directly
user=username
elif user!=username:# If the 1 The user name entered for the second time is not 1 Sample, then on the record 1 The second user name, while counting to zero
user=username
count=0;
for line in block_name:# Check if the user name is locked, and lock returns to the main menu
if username==line.strip('\n'):
print(' This user has been locked. Please contact your administrator ')
main()
password=input('password:')
for line in info:
if(username==line.split('*')[0]and password==line.split('*')[1].strip('\n')):
print(' Login successful! ')
flag2=True
if flag2==False:
count+=1
print(" User name or password is incorrect, please re-enter ")
if count==3:# error 3 Second, add the user name to the lock list
print(' Too many password errors, the user has been locked ')
f3=open('blocklist.txt','a')
f3.write('\n'+username)
f3.close()
count=0# Reset the count after joining the blacklist
main()# Add the blacklist and return to the main menu
info='''
------ Please enter the relevant number -----
1. Create a new user
2. Log in to existing users
3. Exit the program
'''
def main():
print(info)
while flag:
i=input()
if i=='1':
createuser()
break
elif i=='2':
login()
break
elif i=='3':
exit()
else:
print(" Please enter the correct number .")
main()# Program entrance