search
HomeBackend DevelopmentPython TutorialDetailed introduction to the login module in python

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
Merging Lists in Python: Choosing the Right MethodMerging Lists in Python: Choosing the Right MethodMay 14, 2025 am 12:11 AM

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

How to concatenate two lists in python 3?How to concatenate two lists in python 3?May 14, 2025 am 12:09 AM

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Python concatenate list stringsPython concatenate list stringsMay 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Python execution, what is that?Python execution, what is that?May 14, 2025 am 12:06 AM

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Python: what are the key featuresPython: what are the key featuresMay 14, 2025 am 12:02 AM

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment