流程控制值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 ...
随机推荐
- 像屎一样的 Spring Boot入门,总算有反应了
我特么最烦的就是现在Java不知道抽什么风,喜欢用maven这种,怎么搞都会有错误提示的玩意.搞个spring boot,官方的所谓http://start.spring.io/生成的项目启动不了. ...
- 数据库sql常见优化方法
以前刚开始做项目的时候,开发经验尚浅,每次遇到查询比较慢时,项目经理就会问:是不是又用select * 了?查询条件有没有加索引?一语惊醒梦中人,赶紧检查..果然如此! 有时我们写sql语句时,没有考 ...
- [Swift]LeetCode348. 设计井字棋游戏 $ Design Tic-Tac-Toe
Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...
- [Swift]LeetCode598. 范围求和 II | Range Addition II
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- [Swift]LeetCode965. 单值二叉树 | Univalued Binary Tree
A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...
- [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...
- python-类的定制
1.看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的.__slots__我们已经知道怎么用了,__len__()方法我们也知道是为了能让cl ...
- dedecms搜索模板,使用{dede:list}标签调用自定义字段不显示(空白)
前几天使用织梦做一个搜索功能,正常使用{dede:list}调用自定义内容模型中的自定义字段,代码如下:(自定义字段的调用可以参考:http://www.dede58.com/a/dedejq/523 ...
- Python内置函数(22)——float
英文文档: class float([x]) Return a floating point number constructed from a number or string x. If the ...
- Redis学习——Windows环境下Redis的安装(二)
一.说明 之前介绍了Linux环境下Redis的安装,这次介绍一下Windows环境下Redis的安装,首先要讲的是,Redis官方只支持Linux,还好 Microsoft Open Tech gr ...