Python exercises-1. Simple logon interface and python exercises-1 Interface
Function: Used to implement simple user logon interfaces.
Description: User information is stored in the dictionary. You need to manually write the user information to the code. You can log on for a maximum of nine times. The first attempt will first create a lock file in the program directory. If you enter quit in the user column, exit the program.
Enter the user. First, judge whether the user is locked based on the user stored in the lock file. If the user is locked, enter the user again. If the password is not locked, enter the password again. If the password is correct, the message is printed and the password is exited. If the password is incorrect, the number of attempts to the user increases by 1.
After the user's password is wrong three times, the lock file will be written and the program will be exited.
The source code is as follows:
#/Usr/bin/env python
#-*-Coding: UTF-8 -*-
Import OS
User_dic = {
'Jyd ': {'Password': 'abc', 'Count': 0 },
'Test': {'Password': 'test', 'Count': 0}
}
If not OS. path. isfile ('lock '):
Open ('lock', 'w'). close ()
Total_error = 0
Print ("Exit from the username and enter: quit ")
While total_error <9:
User = input ('username --> :')
If user = 'quit ':
Break
If user in user_dic.keys ():
# Check whether the user exists in the lock file. If yes, the user is locked and the value of lock_u is set to yes. Then, the lock judgment loop is displayed. Determine whether the user is locked based on the value of lock_u.
Lock = open ('lock', 'R ')
For lock_u in lock. read (). split ('\ n '):
If user = lock_u:
Print ('user % s is in a locked state' % User)
Lock_u = 'yes'
Break
Lock. close ()
If lock_u = 'yes': continue
Passwd = input ('password --> :')
If passwd = user_dic [user] ['Password']:
Print ('login successfully! ')
Break
Else:
Print ('password error ')
If user_dic [user] ['Count'] <3:
User_dic [user] ['Count'] + = 1
Else:
Lock = open ('lock', 'A ')
Lock. write (user + '\ n ')
Print ('user % s has been locked' % User)
Break
Else:
Print ('username does not exist .')
Total_error + = 1