Home  >  Article  >  Backend Development  >  How to break out of while loop in python

How to break out of while loop in python

silencement
silencementOriginal
2019-06-13 17:49:5545179browse

How to break out of while loop in python

Let’s look at the code first

person={'name':'Helen','password':'123'}

while True:
    nm=input('请输入用户名')
    psw=input('请输入密码')
    if nm==person['name'] and psw==person['password']:
        cmd=input('请输入指令:')
        while cmd!='quit':
            cmd = input('请输入指令:')
        break

Look at another one

#        while True:
#           cmd=input('请输入指令')
#            if cmd=='quit':
#               break
    else:
        print('账号或密码错误\n')
        continue

Summary:
1. While is used to build a loop, while True is an infinite loop;
2. break is used to exit for loops and while loops. When there are multiple loops, exit the loop body where break is located.
3. return is used to end the function and return data, applicable The object is wrong, so an error is reported
4. continue is used to end the current loop body, and return to the loop body where the continue statement is located to start the next loop; in this example, start execution: while True:

The above is the detailed content of How to break out of while loop 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

Related articles

See more