day010 - python implement user login, registration example

python function-oriented programming, simulated user login authentication, registration code.

There are two major documents:

1, user.txt document files, the equivalent of the user information table in the database, mainly to record user names and passwords.

Note: 1) This document needs to .py files in the same path.

   2) user name and password in the memory, is $ symbol to distinguish.

2, the analog user login authentication, registration code.

. 1  # ! / Usr / bin / Python the env 
2  # - * - Coding: UTF-. 8 - * - 
. 3  
. 4  DEF Login (username, password):
 . 5      "" " 
. 6      for user login authentication
 . 7      : param username: username
 . 8      : param password: password
 . 9      : return: True, the login is successful; False, login failure.
 10      "" " 
. 11      with Open ( " user.txt " , " R & lt " , encoding = " UTF-. 8 " ) AS F:
 12 is          for Line in f:
 13             = line.strip Line ()   # default parameters without strip, will remove both spaces, line breaks; the removal of the specified parameter value 
14              line_list line.split = ( " $ " )    # to $ symbol extraction username and password 
15              IF username = line_list = [0] and password == line_list [. 1 ]:
 16                  return True
 . 17              the else :
 18 is                  return False
 . 19  
20 is  DEF Register (username, password):
 21 is      "" " 
22 is      the user registration
 23 is      : param username: user name
 24      : param password: password
 25     : return: True, successfully registered
 26      "" " 
27      with Open ( " user.txt " , " A " , encoding = " UTF-8 " ) AS f:
 28          the TEMP = " \ the n- " + username + " $ " + password    #   "\ n-" newline 
29          f.write (TEMP)
 30      return True
 31 is  
32  DEF user_exsit (username):
 33 is      "" " 
34 is      the time of registration, it is determined whether there is a user name
 35      :
param username: username 36     :return:True, 用户名已存在
37     """
38     with open("user.txt", "r", encoding="utf-8") as f:
39         for line in f:
40             line = line.strip()
41             line_list = line.split("$")
42             if username == line_list[0]:
43                 return True
44         return False
45 
46 def main():
47     Print ( " Welcome to use the system, enter your operating options. " )
 48      InP = the INPUT ( " 1. Log; 2 registered, please enter the number:.. " )
 49      IF InP == " 1 " :
 50          Times = . 1
 51 is          the while True:
 52 is              IF times ==. 4 :
 53 is                  Print ( " enter three user name or password is incorrect, please try again after an hour. " )
 54 is                  BREAK 
55              user = iNPUT ( " Please enter your user name: " )
 56 is              pwd = INPUT ( "Please enter your password: " )
 57              is_login = the Login (the User, pwd)
 58              IF is_login:
 59                  Print ( " Congratulations on your system login is successful!. " )
 60              the else :
 61                  Print ( " . Username or password is incorrect " )
 62              Times + 1 =
 63      IF InP == " 2 " :
 64-          the user = the iNPUT ( " Please enter your user name: " )
 65          pwd = the iNPUT ( " Please enter your password: " )
66          IF user_exsit (the User):
 67              Print ( " user name already exists, registration failed! " )
 68          the else :
 69              RET = the Register (the User, pwd)
 70              IF RET:
 71                  Print ( " ! Registered successfully " )
 72              the else :
 73                  Print ( " registration failed! " )
 74  
75 main ()
View Code

 

Guess you like

Origin www.cnblogs.com/june-L/p/11601069.html