流程控制值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 ...
随机推荐
- win 10 亮度调节不能使用了
我的解决办法的前提:装过teamviewer ,然后每次系统推送大升级似乎都会无法调节亮度,如果不是这个前提自己找别的办法吧 teamviewer 就是一个流氓软件. 每次更新之后都末名奇妙的不能调节 ...
- Data Center手册(4):设计
基础架构 拓扑图 Switching Path L3 routing at aggregation layer L2 switching at access layer L3 switch融合了三种功 ...
- [Swift]LeetCode216. 组合总和 III | Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [Swift]LeetCode922.按奇偶排序数组 II | Sort Array By Parity II
Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...
- [Swift]LeetCode982. 按位与为零的三元组 | Triples with Bitwise AND Equal To Zero
Given an array of integers A, find the number of triples of indices (i, j, k) such that: 0 <= i & ...
- Spring及SpringBoot @Async配置步骤及注意事项
前言 最近在做一个用户反馈的功能,就是当用户反馈意见或建议后服务端将意见保存然后发邮件给相关模块的开发者.考虑发邮件耗时的情况所以我想用异步的方法去执行,于是就开始研究Spring的@Async了.但 ...
- Java的内部类真的那么难以理解?
01 前言 昨天晚上,我把车停好以后就回家了.回家后才发现手机落在车里面了,但外面太冷,冷到骨头都能感受到寒意——实在是不想返回一趟去取了(小区的安保还不错,不用担心被砸车玻璃),于是打定主意过几个小 ...
- JVM基础系列开篇:为什么要学虚拟机?
跟许多人一样,我一开始接触 Java 虚拟机只是因为面试需要用到,所以硬着头皮看看.所以很多人对于为什么要学虚拟机这个问题,他们的答案都是:因为面试.但我经过了几年的学习和实战,我发现其实学习虚拟机并 ...
- 『Raid 平面最近点对』
平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...
- centos7 修改yum源为阿里源
centos7 修改yum源为阿里源,某下网络下速度比较快 首先是到yum源设置文件夹里 安装base reop源 cd /etc/yum.repos.d 接着备份旧的配置文件 sudo mv Cen ...