One, user login requirements:
1, user name, password to be saved in the file.
2, the user can try to log in three times, three times after the account lockout, the user lock information saved in the file, the user input to reset the lock information correctly.
Program Code :
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Username = [] #-----------------------------------------------
For line in open (' User.txt '):
Line=line.replace (' \ n ', ') '. Split () Remove the user name from the #从user. txt file and save it to the list username
Username.append (Line[0]) #-----------------------------------------------
Password = [] #-----------------------------------------------
For line in open (' User.txt '):
Line=line.replace (' \ n ', '). Split () #从user. txt file to remove the password and save it in the list password
Password.append (Line[1]) #-----------------------------------------------
Counter = [] #-----------------------------------------------
For line in open (' User.txt '):
Line=line.replace (' \ n ', '). Split () Remove user account status #从user. txt file and save to list counter
Counter.append (Line[2]) #-----------------------------------------------
New_counter = [] #-----------------------------------------------
For N in Counter:
New_counter.append (int (n)) #将counter中的元素转换成数值型字符
counter = new_counter #-----------------------------------------------
Flag = True
While flag:
If min (counter) < 3: #判断所有用户的状态, allows the program to run as long as a user can log on
Usname = input ("Please enter user name:")
If usname in Username: #如果输入的用户名在用户列表中
p = Username.index (usname) #确定输入的用户名在列表中的位置
While True:
If COUNTER[P] < 3: #判断所选用户是否锁定
Psword = input ("Please enter password:")
if Psword = = Password[p]:
Print ("Congratulations! User name, password input is correct! ")
Counter[p] = 0 #-----------------------------------------------
NewUser = open ("User.txt", ' W ')
For I in range (len (username)): #用户密码输入正确, resets the user state, and writes the user file
Newuser.write ('%s%s%d\n '% (Username[i],password[i],counter[i]))
Newuser.close () #-----------------------------------------------
Flag = False #将最外层循环的值置为假, used to exit the final loop
Break #退出当前循环
Else
Print ("User name password is wrong ...")
COUNTER[P] + = 1 #计数器自加1
NewUser = open ("User.txt", ' W ') #-----------------------------------------------
For I in range (len (username)): #将用户错误次数写入用户文件
Newuser.write ('%s%s%d\n '% (Username[i],password[i],counter[i]))
Newuser.close () #-----------------------------------------------
Break
Else
Print ("This user is locked ...") #判断用户状态为锁定后提示用户已定
Break
Else
Print ("No this user, re-enter") #用户名输入错误, prompting the user to re-enter
Else
Print ("All users are locked, unable to open programs ...") #判断所有用户状态均已锁定, program exits
Break
User files:
Songyang sy123 0 #第一列为用户名, second column password, third column user status
Admin ad123 0
Program Important location Explanation:
Username = [] #创建一个名称为username的空列表
For lines in open (' User.txt '): #遍历user. txt file in each row
Line=line.replace (' \ n ', '). Split () #将user all values for each row in the. txt file are stored in the line list, replace () function
#是将每行结尾的换行符替换成空格, the split () function divides each line of a file into a list with a space symbol
Username.append (line[0]) #将line列表中的第一个元素保存到username列表中 to form a list of user names
NewUser = open ("User.txt", ' W ') #以写方式打开用户文件, when the contents of the file are read into memory, the program deletes the file contents
For I in range (len (username)): #程序检测username列表有几个元素, loop several times
Newuser.write ('%s%s%d\n '% (Username[i],password[i],counter[i])) #将所有列表username, the contents of the Password,counter are written to the file
Newuser.close () #关闭文件
program Run error explanation :
This is the corresponding selection, loop and other statements after forgetting to add ":" Causes
This is caused by no indentation for the corresponding error line.
Python learning one: User Login