流程控制值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 ...
随机推荐
- 开发自定义ScriptableRenderPipeline,将DrawCall降低180倍
0x00 前言 大家都知道,Unity在2018版本中正式推出了Scriptable Render Pipeline.我们既可以通过Package Manager下载使用Unity预先创建好的Ligh ...
- 【RL-TCPnet网络教程】第20章 RL-TCPnet之BSD Socket客户端
第20章 RL-TCPnet之BSD Socket客户端 本章节为大家讲解RL-TCPnet的BSD Socket,学习本章节前,务必要优先学习第18章的Socket基础知识.有了这些基础知 ...
- 字符串----hiho字符串(尺取法)
注意:这道题的解法和最短摘要一样,都是采用尺取法解决问题,注意这儿题目要求恰好包含,也就是说这个hiho字符串必须包含2个'h'.1个'i'和1个'o'.一个不能多,一个也不能少. import ja ...
- [Swift]LeetCode691. 贴纸拼词 | Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- [Swift]LeetCode771. 宝石与石头 | Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- Python -- socket 实现服务器之间的通信
现在需要做一个分布式课程设计(简单小游戏),三个人小组合作完成. 我需要设计一个登录注册服务器,接收来自网关服务器(消息中间件)的用户登录注册消息请求,然后生成访问数据库服务器的消息,发送给数据库服务 ...
- Python时间和时间戳互相转换
# 将时间变成时间戳 def tranftimestamp(stringtime): try: return time.mktime(time.strptime(stringtime, "% ...
- asp.net core系列 27 EF模型配置(索引,备用键,继承)
一.索引 索引是许多数据存储中的常见概念.虽然它们在数据存储中的实现可能会有所不同,但它们可用于更有效地基于列(或列集)进行查找.按照约定,用作外键每个属性 (或组的属性) 会自动创建索引.无法使用数 ...
- redis 系列19 客户端
一. 概述 Redis服务器是可以与多个客户端建立网络连接,每个客户端可以向服务器发送命令请求,而服务器则接收并处理客户端发送的命令请求,并向客户端返回命令回复.通过使用I/O多路复用技术实现的文件事 ...
- SpringMvc通过@Value( ) 给静态变量注入值
spring 不允许/不支持把值注入到静态变量中,如: @Value("${ES.CLUSTER_NAME}")private static String CLUSTER_NAME ...