Brief introduction
This project login registration verification is to learn the prior learning point of deepening learning, the project's knowledge points have functions, judgment statements, circular statements, file operations.
Project Flow
After running the code, output the login or registration options.
When the login is selected, enter the user name password, if the user information is not registered, the output login failed, if the user information has been registered, will show the login success.
When the registration is selected, enter the user name password, if the user information is not registered, the output registration is successful, if the user information has been registered, the display user name already exists, registration failed.
Code flow
Run generation after the first execution of the main () function, the Mian () function to receive user input user name and password, the user chooses to log in, call the Loing () function, the user input information in and the information stored in the user information in the file, if there is a user information on the login success, or failure, The user chooses to register, after receives the user input user name password, calls the register () function, here will call the User_exist () function to judge the user input information, if the user input information exists in the file which holds the user information, the output registration fails, otherwise the registration succeeds.
An error handler was called in the login () and user_exist () functions.
Introduction to Error handling functions
Try :..... except IOError: ...
Code execution in try no longer executes the code in except and executes code in except if the code in the try is in error.
Code implementation
defLogin (username, password):"""authentication for user name and password:p Aram Username: User name:p Aram Paaword: Password: return:true, user authentication succeeded; False, user authentication failed""" Try: F= Open ("Users","R", encoding="Utf-8") forLineinchF:line= Line.strip ()#Clear Line Break #remove both spaces when no parameters are removed, line breaks #remove characters specified on both sides when parameters are availableLine_list = Line.split ("$") ifLine_list[0] = = Username andLINE_LIST[1] = =Password:#Print ("Success") returnTruereturnFalseexceptIOError:returnFalsedefRegister (username, password):"""Registered user 1, open file 2, user name $ password:p Aram Username: User name:p aram Password: password: return:true: registration success;"""With Open ("Users","a", encoding="Utf-8") as F:temp="\ n"+ Username +"$"+Password F.write (temp)returnTruedefuser_exist (username):"""detects if a user name exists:p Aram Username: User name to detect: Return:true: username exists; False: User name does not exist""" #a row to find, if the user name exists, return True:false Try: With open ("Users","R", encoding="Utf-8") as F: forLineinchF:line=Line.strip () line_new= Line.split ("$") ifLine_new[0] = =Username:returnTruereturnFalseexceptIOError:returnFalsedefMain (): whileTrue:Print("Welcome to login XXX System") Arg= Input ("1: Login, 2: Register") ifarg = ="1": Uer= Input ("Please enter user name:") PWD= Input ("Please enter your password:") ifLogin (Uer, pwd):Print("Success") Break Else: Print("failed") elifarg = ="2": User= Input ("Please enter user name:") PWD= Input ("Please enter your password:") Is_exist=user_exist (user)ifis_exist:Print("user name already exists, registration failed") Else: ifregister (user, pwd):Print("Registration Successful") Continue Else: Print("Registration Failed") Else: Print("input Error, please re-enter") Main ()
This project only uses some of the previously learned knowledge, a relatively simple project, and then continue to deepen learning.
Python learning: 8. Login registration verification of small projects