流程控制值while 循环
一.结束循环的两种方式
1. 修改条件
tag=True
while tag:
print(1)
print(2)
print(3)
tag=False
print(4)
2.while + break 直接结束本次循环
while True:
while True:
while True:
break
break
break 案列一:特点是在输错三次后还可以打印 提示输入次数过多
name='deng'
pwd='123'
count=0
while True:
name_inp=input('please input your name:')
pwd_inp=input('please input your password:')
if name_inp == name and pwd_inp ==pwd:
print('login successful')
break
else:
print('user or password error')
count+=1
if count ==3:
print('too many error')
break 案列二:特点是在输错三次后直接结束循环
name='deng'
pwd='123'
count=0
while count < 3:
name_inp = input('please input your name:')
pwd_inp=input('please input your password:')
if name_inp == name and pwd_inp == pwd:
print('login successsful')
break
else:
print('user or password error')
count+=1 二. while + continue
continue 代表结束本次循环,直接进入下一次循环
案列一:0到10之间所有的数字,除了5,其余的分别打印出来
n=0
while n < 10:
if n == 5:
n+=1
continue
print(n)
n+=1 不合理示范1
while True:
print(1)
print(2)
print(3)
continue 不合理示范2
while True:
print(1)
print(2)
if 1 == 2:
pass
else:
print('xxxx')
continue 三.while嵌套
案列一
name='deng'
pwd='123'
count=0
while True:
name_inp=input('please input your name:')
pwd_inp=input('please input your password:')
if name_inp == name and pwd_inp == pwd:
print('login successful')
while True:
print('''
0 退出
1 取款
2 转账
3 查询
''')
cmd = input('请输入指令编号:')
if cmd == '0':
break
elif cmd == '1':
print('取款')
elif cmd == '2':
print('转账')
elif cmd == '3':
print('查询')
else:
print('输入的指令错误,请重新输入!!!')
break
else:
print('user or password error')
count+=1
if count == 3:
print('输错次数过多!!!')
break 案例二:
name='egon'
pwd='123'
count=0
tag=True
while tag:
inp_name=input('please input your name: ')
inp_pwd=input('please input your password: ')
if inp_name == name and inp_pwd == pwd:
print('login successful') while tag:
print("""
0 退出
1 取款
2 转账
3 查询
""")
cmd = input('请输入指令编号>>>: ') # cmd='0'
if cmd == '0':
tag=False
elif cmd == '1':
print('取款...')
elif cmd == '2':
print('转账...')
elif cmd == '3':
print('查询...')
else:
print("输入错误指令,请重新输入") else:
print('user or passwor error')
count+=1 #count=3 if count == 3:
print('too many tries.....')
break
三:while+else
else:如果while循环没有被break打断过,即正常运行完毕后才会执行else的子代码块
n=0
while True:
# if n == 3:
# break
print(n)
n+=1
else:
print('run.....') n=0
while n <= 3:
print(n)
n+=1
else:
print('run.....')
流程控制值while 循环的更多相关文章
- SSIS从理论到实战,再到应用(4)----流程控制之For循环
原文:SSIS从理论到实战,再到应用(4)----流程控制之For循环 上期回顾: SSIS从理论到实战,再到应用(3)----SSIS包的变量,约束,常用容器 在SSIS体系中,控制流可能经常会遇到 ...
- SSIS从理论到实战,再到应用(5)----流程控制之Foreach循环
原文:SSIS从理论到实战,再到应用(5)----流程控制之Foreach循环 上期回顾: SSIS从理论到实战,再到应用(4)----流程控制之For循环 上一期讲了For循环,Foreach循环相 ...
- [转帖]流程控制:for 循环
流程控制:for 循环 http://wiki.jikexueyuan.com/project/linux-command/chap34.html need more study need more ...
- 流程控制之while循环for循环
流程控制之while循环1.什么是循环 循环就是重复做某件事2.为什么要有循环 为了让计算机能够具备人重复做某件事的能力3.如何用循环 while语法: while 条件: code1 code2 c ...
- day04流程控制之while循环
流程控制之while循环 1.什么是while循环 循环指的是一个重复做某件事的过程 2.为何有循环 为了让计算机能像人一样重复 做某件事 3.如何用循环 ''' # while循环的语法:while ...
- php总结3——基本函数、流程控制中的循环
3.1 php基本函数(数学.日期.字符串) 数学函数:max mixed max(number $arg1,number $arg2,……) 求一组数据中的最大值 m ...
- 流程控制之 for 循环
目录 流程控制之for循环 for 循环条件语句 for 循环的嵌套 流程控制之for循环 for 循环条件语句 for i in range(3): print(i) # 0 # 1 # 2 for ...
- (16)JavaScript的流程控制(js的循环)
流程控制有3种结构 1.顺序结构:代码执行的本质就是顺序结构 2.分支结构:if家族 语法规则: if (条件1) { //代码块1}else if (条件2){ //代码块1}//如果所有条件都不满 ...
- (2)流程控制(for循环、if...else判断、while循环)
for循环 for item in names: #结构语法 print(item) for循环嵌套for循环 for循环配合range()可以直接指定要打印的数量 例:打印一个金字塔 for i ...
随机推荐
- RabbitMQ in Action (2): Running and administering Rabbit
Server management the Erlang node and the Erlang application Starting nodes multiple Erlang applicat ...
- Razor Page Library:开发独立通用RPL(内嵌wwwroot资源文件夹)
ASP.NET Core知多少系列:总体介绍及目录 Demo路径:GitHub-RPL.Demo 1. Introduction Razor Page Library 是ASP.NET Core 2. ...
- jenkins自动化工具使用教程
自动化构建.测试.部署.代码检测越来越重要.主要有一下几点原因 1. 企业做大,项目变多,多端支持(web,h5,小程序等) 2. 微服务提倡高内聚低耦合,项目因拆分变多 3. DevOps自动 ...
- Thread.join(), CountDownLatch、CyclicBarrier和 Semaphore区别,联系及应用
在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法, 由于 ...
- [Swift]LeetCode81. 搜索旋转排序数组 II | Search in Rotated Sorted Array II
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [Swift]LeetCode837. 新21点 | New 21 Game
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 p ...
- [Swift]LeetCode960. 删列造序 III | Delete Columns to Make Sorted III
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose an ...
- iReport 5.6.0 Error: net.sf.jasperreports.engine.JRException: Error executing SQL statement for : data 最优解决方案
问题描述 近期学习iReport(个人使用的是最新版本的 iReport-5.6.0,MySQL是 5.5.56版本),遇到一些问题,在安装完成后,创建了数据库,配置了MySQL数据库连接信息,新建报 ...
- tensorflow 1.0 学习:用别人训练好的模型来进行图像分类
谷歌在大型图像数据库ImageNet上训练好了一个Inception-v3模型,这个模型我们可以直接用来进来图像分类. 下载地址:https://storage.googleapis.com/down ...
- php的四个fetch语句
先给一个表 man: |---------------| |-name--|-age--| |--AA---|--aa---| |--BB---|--bb---| |--CC---|--cc---| ...