This example for everyone to share the user login system Python implementation code for your reference, the specific content as follows
Precautions:
1. Use Python3 to execute the program. Enter 1 or 2 as prompted to sign in or out of the program
2, after entering the user name, if the user name is locked and invalid user name, the program will exit, please ensure that the input is correct.
3, enter the user name correctly, enter the password. If the user name is correct, the password is incorrectly three times, the user will be locked and the system is not allowed to log in.
#!/usr/local/env python#coding:utf-8#auto:panwenbin#function:lock_file=open (' file_lock.txt ', ' r+ ') user_pass=open (' Username_file.txt ', ' r+ ') count=0
Cmd=input ("' 1: Login System 2: Exit System Please enter your action:") if Cmd.isdigit () and int (cmd) ==2:exit () elif cmd.isdigit () and int (cmd) ==1: While Count <3: #只要重试不超过3次就不断循环 username=input (' Please enter your user name: ') for I in Lock_file.readlines (): #判断用户名是否在锁文件中 i=i.split () if username in i[0]: print (' Sorry%s locked '%username) exit ()
Match = False for J in User_pass.readlines (): User,password = J.strip (' \ n '). Split () #去掉每行多余的 \ n and divide the line into two columns, Assign a value of USER,PASSWD two variables if username = = User: #判断输入的用户是否存在 passwd=input (' Please enter password: ') if password = = passwd: print (' username and password correct ') match = True break elif Password! = passwd: #在用户名正确的前提下, determine if the password entered is correct for i in Range (2): passwd=input (' Password wrong, re-enter password: ') if password = = passwd: print (' username and password correct ') match = True Break
If username! = User: #判断用户不存在 print (' You enter user name does not exist, program has exited ') exit () elif match = = False: #如果match还为False, On behalf of the above loop, there is no match on the username and password print (' Password and username mismatch, attempt more than three times, user is locked ') lock_file.write ('%s \ n '%username) Lock_ File.close () user_pass.close () exit () elif match==true: print (' login successful ') Break else: Print (' invalid option, program exited ')
The above is the whole content of this article, I hope that you learn Python programming help.