Home  >  Article  >  Backend Development  >  Detailed introduction to the login module in python

Detailed introduction to the login module in python

零下一度
零下一度Original
2017-07-21 14:19:243246browse

Login module:

No matter which website we go to, we often encounter this situation. Let us log in to this website. The flow chart is as follows:

Ideas:

1. When we log in to the website, we first enter the user name , at this time, some websites will remind us whether the user name exists. If the user name we enter does not exist, a prompt will appear telling us that the user name does not exist. At this time, we need to re-enter it or choose to register. Of course, We just ask the user to re-enter here;

2. If the username exists, you need to enter the password. We know that when entering the password, we do not verify whether the password is correct. If Directly verifying whether the password is correct first will lose the role of letting the user enter the verification code. Therefore, we always do not verify the password first, but let the user enter the verification code, and first verify whether the verification code entered by the user is correct;

 3. If the verification code is incorrect, ask the user to re-enter the verification code; if the verification code is correct, then it is time to return to re-verify whether the password is correct; if the password is correct, the login is successful. ; Otherwise, if the password is incorrect, the user needs to re-enter the password at this time. Since the user has already entered the user name at this time, there is no need to re-enter the user name, only the password and verification code are required; that is, Re-enter the password, and then enter the verification code; if the password is correct, the login is successful, otherwise re-enter the password and verification code. The verification code must be entered every time you enter the password;

To achieve The function of the above code must use a while loop. Since it is checked layer by layer, the input of the user name must be a loop until the user enters the correct verification code. The key is how to ensure that the user name is correct when the password or verification code is used. When you make a mistake, you don’t need to re-enter the username, otherwise it will be looked down upon. At this time, you need to consider that when the username is correct, you need to stop the cycle and the next time the user enters the password or verification code, it cannot be executed. When cyclically entering user names.

The code is implemented as follows:

 

def login(name,password):'''用户登录模块,用户登录时候的各种设置,本次实现功能''''''用户登录验证的时候,一般会先判断验证码是否正确,因此要让用户首先验证验证码'''active = Truewhile True:while active:
            username = input("请输入你的用户名:")'''首先验证用户名是否存在'''users = []for user_list in userfile.usernames:
                users.append(user_list[0])if username in users:
                active = Falseelse:
                print("对不起,您输入的用户名不存在,请重新输入:")
        pwd = input("请输入你的密码:")while True:'''加入一个用户输入验证码的模块,让用户输入验证码'''verification_code = str(random.randint(0,9)) + chr(random.randint(65,90)) + str(random.randint(0,9)) + chr(random.randint(97,122))
            print(verification_code)
            test_num = input("请输入验证码:")if test_num == verification_code.lower() or test_num == verification_code.upper():'''无论用户输入大小写都可以验证成功''''''用户验证成功后,开始进行用户自己的认证,是否注册,或者用户名正确'''breakelse:
                print("您输入的验证码有误,请重新输入!")if [username,int(pwd)] in userfile.usernames:
            print("Successful!")return (username,pwd)else:
            print("对不起,您输入的密码有误,请重新输入:")

The above code Run as follows:

请输入你的用户名:geng
请输入你的密码:1233D0t
请输入验证码:3d0t
对不起,您输入的密码有误,请重新输入:
请输入你的密码:2228I9z
请输入验证码:8888您输入的验证码有误,请重新输入!
2G0j
请输入验证码:8888您输入的验证码有误,请重新输入!
7T1a
请输入验证码:7t1a
对不起,您输入的密码有误,请重新输入:
请输入你的密码:22227X4q
请输入验证码:7x4q
对不起,您输入的密码有误,请重新输入:
请输入你的密码:6662O0q
请输入验证码:2o0q
Successful!('geng', '666')

The execution result of the above code is as shown above. When we want to stop a loop alone, we can set an identifier for this loop to open or close the loop. When the conditions are met, close this loop. Loop, so that this loop will not be opened when executing other programs;

active = True
while True:
while active ; #'' '
Users = [] for User_list in Userfile.usernames:
Users.appnd (User_list [ 0]) # #if username in users:
                      active = False
               else:                     
print("
Sorry, the username you entered does not exist , please re-enter: ") ​​ pwd =
input("
Please enter your password: ")

With the above code, we have achieved such a function. When we want to stop this cycle, we Let the identifier of the inner loop be closed.

The above is the detailed content of Detailed introduction to the login module in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn