How to break out of while loop in python

silencement
Release: 2019-06-13 17:49:55
Original
45098 people have browsed it

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
Copy after login

Look at another one

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!