In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how Python implements the login function. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The details are as follows
This article is to use Python to write a registration login function, it is not difficult, and it is very suitable for hand practice.
Mainly using lists and dictionaries, as well as logical judgment.
The third-party library modules used are the time module and the re (regular expression) module.
The purpose of using the time module here is to make the program run with a sense of progress through the sleep method, instead of popping up all the information at once.
The use of the re module is used to determine the complexity of passwords, and our registration software system determines whether our passwords are complex enough by regular expressions.
The first step is to import these two modules, that is,
Import timeimport re
After that, you need a list to store the registered id, and you need a dictionary to bind the ID to the password, because the dictionary is in key:value format, with ID as the key and password as the value.
The code can be represented as
User_id= [] # Storage user iduser_dict= {} # bind id to password
After that, you have to ask the user whether he has registered an account. If not, prompt the user to sign up for the account. You can receive the user's choice through a variable, and then perform the corresponding operation.
Choice=input ('whether you have registered an account or not, please register 1: register 2: log in 3: log out of the system')
Enter 1 to enter the registration interface. First of all, you need to enter a user name and password, and finally prompt the user to register successfully.
Then you need to add the user's id to the user_id and store the user's id and password in a dictionary, that is,
User_name=input ('Please enter your user name') password=input ('Please enter your password') user_id.append (user_name) for item in user_id: if item not in user_dict: user_ entry [item] = passwordprint ('registered successfully!)
Registration requires login, which is a little more troublesome than registration.
First of all, you need to determine whether the id entered by the user has been registered (that is, whether the entered id is in the user_id)
After that, if it exists, you need to match whether the password is correct or not before you can log in.
However, the user may forget the password, although we can check the password through the dictionary and print it to the user, but generally not recommended, try to recommend the user to change the password, and the user entered the unregistered ID, need to advise the user to sign up to improve efficiency.
Only i in list is needed to determine whether the element is in the list. If true, the element exists in the list, and vice versa.
While 1: name=input ('Please enter your user name') key=input ('Please enter your password') if name in user_id: if user_ password [name] = = key: print ('login successful') break else: print ('Please check your password error') q=input ('do you choose to change your password entry? Y change input other exit') if qpassword password exit: new_password=input ('Please enter a new password') user_ password [name] = new_password print ('password modified successfully!') Else: break else: print ('user name does not exist, please check to enter') r=input ('do you need to register account 1 to register for other exits') if registration registration account: user_login=input ('Please enter the user name to register') password_login=input ('Please enter your login password ') user_id.append (user_login) for num in user_id: if num not in user_dict: user_ subscription [user _ login] = password_login print (' registered successfully') else: break
It is worth noting that we must first judge whether the id exists, not the password directly, because the unregistered id does not have a corresponding key in the dictionary and will report an error Keyerror
After that, it is recommended to exit the program with 5s buffer, so that you can use the time.sleep () method all of a sudden.
Print ('Thank you for your use, the program will exit in 5 seconds') for i in range (5): time.sleep (1) print ('the program will exit in {} seconds' .format (5muri))
At this point, the project function is complete, and the following is the source code of the project:
There is no regular expression above, and there is a regular expression in the source code to determine whether it contains letters or not.
Import timeprint ('- Thank you for using this system -') user_id= [] user_dict= {} time.sleep (1) while 1: choice=input ('if you have not registered an account, please register 1: register 2: log in 3: log out of the system') if choice=='1': user_name=input ('Please enter your user name') while 1: password=input ('Please enter the password needs to contain letters') p=re.compile ('^. * [Amurz Amurz] +. * $') if p.match (password): break else: print ('password is not complex enough to re-enter') user_id.append (user_name ) for item in user_id: if item not in user_dict: user_ user_ [item] = password elif choice=='2': while 1: name=input ('Please enter your user name') key=input ('Please enter your password') if name in user_id: if user_ entry [name] = = key: Print ('login successful') break else: print ('password error, please check your input') q=input ('do you choose to change password input y change input other exit') if qcharacters: While 1: new_password1=input ('Please enter the new password') new_password2=input ('Please confirm the new password') if new_password_1==new_password2: user_ password [name] = new_password1 Break else: print ('two passwords are inconsistent Please re-enter') print ('password modified successfully') Else: break else: print ('user name does not exist, please check the input') r=input ('do you need to register an account by entering 1 to register for other exits') if ritual registration account 1: user_login=input ('Please enter the user name to register ') password_login=input (' Please enter your login password') user_id.append (user_login) for num in user_id: if num not in user_dict: user_ login [user _ login] = password_login print ('registered successfully') else: break else: print ('Thank you for your use The program will exit in 5 seconds.') for i in range (1) print: time.sleep (1) print ('the program will exit in {} seconds'. Format (5mai) Thank you for reading! This is the end of this article on "how to achieve the registration and login function of Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.