Python Exercise 1 -- User Login, python1 -- Login
1. Requirements
1) enter the user name and password
2) The welcome information is displayed after the authentication is successful.
3) Lock after three wrong attempts
2. Demand Analysis
1) User information is stored in the file (login/config/user_login.txt)
2) enter the user name and password
3) Determine whether the user name exists. If the user name exists, continue. If the user name does not exist, continue to enter the user name.
4) Determine whether the user name has been locked. If the user name has been locked, exit the program. Otherwise, continue.
5) match the user information in the file
6) If yes, the welcome information is printed.
7) if three wrong passwords are entered, the user name (login/config/name_lock.txt) will be locked)
3. Test the user
bigberg:123abc
lc:123456
smallberg:111111
root:12345
dinasor:12321
1 #-*-coding: UTF-8-*-2 # Author: bigberg 3 4 # define a cyclic count 5 count = 0 6 # define a dictionary storage username and password 7 names = {} 8 9 # define a list storage username 10 name_exit = [] 11 12 # enter username 13 username = input ("username: ") 14 15 # determine whether the user entered is in the User Name List 16 with open (".. /config/name_login.txt ", 'R') as f: 17 while True: 18 line = f. readline (). strip () 19 if not line: 20 break21 else: 22 name = line. split (':') [0] 23 passwd = line. split (':') [1] 24 names [name] = Passwd25 for key in names: 26 name_exit.append (key) 27 # determine whether The user name is correct. If The user name is incorrect, enter 28 while username not in name_exit: 29 print ("The account is not exit. check it again. ") 30 username = input (" username: ") 31 else: 32 # Read the content of the locked file 33 with open (".. /config/name_lock.txt "," r ") as f: 34 lock_name = f. read () 35 36 # determine whether the user name is in the locked file. if the user name is in the locked file, exit the program 37 if username = lock_name: 38 print ("Sorry. your account has been locked. ") 39 exit () 40 else: 41 #3 password input opportunities 42 while count <:43 passwd_input = input ("password :") 44 45 # determine whether the user name and password are valid 46 if passwd_input = names [username]: 47 print ("Welcome! ", Username) 48 break49 else: 50 print (" Error, please try again. ") 51 52 count + = 153 54 # if three wrong passwords are entered, add the user name to the locked file 55 if count = with open (".. /config/name_lock.txt "," w ") as f: 57 f. write ("% s" % username) 58 print ("You have tried 3 times, and your account will be locked ")