流程控制if、while、for
if判断
if判断想执行第一个条件,if后的判断必须是True
1 什么是if判断
判断一个条件如果成立则做...不成立则做....
2 为何要有if判断
让计算机能够像人一样具有判断的能力
3 如何用if判断
语法1:
if 条件1:
code1
code2
code3
......
语法2:if-else
if 条件:
code1
code2
code3
......
else:
code1
code2
code3
......
语法3:嵌套
if 条件1:
if 条件2:
code1
code2
code3
code2
code3
......
语法4:elif
if 条件1:
子代码块1
elif 条件2:
子代码块2
elif 条件3:
子代码块3
elif 条件4:
子代码块4
.........
else:
子代码块5
代码练习
score = input('your score>>: ')
score=int(score)
if score >= 90: #不满足的必然小于90,elif只需要>=80就行,不用判断与90的关系
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')
while循环判断
1. 什么是循环
循环指的是一个重复做某件事的过程
2. 为何要有循环
为了让计算机能够像人一样重复做某件事
3. 如何用循环
while循环的语法:while循环又称为条件循环,循环的次数取决于条件
while 条件:
子代码1
子代码2
子代码3
登录账号密码验证:
print('start....')
while True:
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
else:
print('user or password err')
print('end...')
结束while循环的方式:
方式一:操作while循环的条件让其结束
控制条件的两种方式:
判断count:
count=1
while count < 6:
print(count)
count+=1 判断布尔
tag=True
count=1
while tag:
if count > 5:
tag=False
print(count)
count+=1
print('start....')
tag=True
while tag:
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
tag=False
else:
print('user or password err')
print('end...')
方式二: break强行终止本层循环
print('start....')
while True:
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
break
else:
print('user or password err')
print('end...')
输错三次账号或密码直接退出:
方式一:
print('start....')
count=0
while count <= 2: #count=3
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
break
else:
print('user or password err')
count+=1 print('end...') 方式二:
print('start....')
count=0
while True:
if count == 3:
print('输错的次数过多傻叉')
break
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
break
else:
print('user or password err')
count+=1 print('end...')
continue :结束本次循环,直接进入下一次
break :结束本层循环
count=1
while count < 6:
if count == 4:
count+=1 #如果不加一,会进入死循环
continue # 只能在cotinue同一级别之前加代码,之后的代码不会执行
print(count) #print 不能放在if之前,否则排除不了4
count+=1 while True:
print('')
print('')
print('')
continue # 不应该将continue作为循环体最后一步执行的代码反正也会重新开始循环,相当于多此一举
while+else
count=1
while count < 6:
if count == 4:
break
print(count)
count+=1
else:
print('会在while循环没有被break终止的情况下执行')#如果while循环被终止了的话,else后的代码不会执行
输错三次则退出之while+else的应用代码:
print('start....')
count=0
while count <= 2: #count=0,1,2 3的时候是第四次
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
break
else:
print('user or password err')
count+=1
else:
print('输错的次数过多')
print('end...')
while循环的嵌套,内部循环输入4,直接退出程序,即退出两个循环 复杂版:
name_of_db='egon'
pwd_of_db=''
print('start....')
count=0
while count <= 2: #count=3
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == name_of_db and pwd == pwd_of_db:
print('login successful')
while True:
print("""
1 浏览商品
2 添加购物车
3 支付
4 退出
""")
choice=input('请输入你的操作: ') #choice='1'
if choice == '':
print('开始浏览商品....')
elif choice == '':
print('正在添加购物车....')
elif choice == '':
print('正在支付....')
elif choice == '':
break
break
else:
print('user or password err')
count+=1
else:
print('输错的次数过多') print('end...')
简洁版:定义tag变量控制所有循环
name_of_db='egon'
pwd_of_db=''
tag=True
print('start....')
count=0
while tag:
if count == 3:
print('尝试次数过多')
break
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == name_of_db and pwd == pwd_of_db:
print('login successful')
while tag:
print("""
1 浏览商品
2 添加购物车
3 支付
4 退出
""")
choice=input('请输入你的操作: ') #choice='1'
if choice == '':
print('开始浏览商品....')
elif choice == '':
print('正在添加购物车....')
elif choice == '':
print('正在支付....')
elif choice == '':
tag=False else:
print('user or password err')
count+=1 print('end...')
for循环主要用于循环取值
普通方法:
student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']
i=0
while i < len(student): #len为列表的方法,相当于Java中数组的长度方法
print(student[i])
i+=1
用for循环取值:
student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb'] #打印每一个列表元素
for item in student:
print(item) #打印字符串的每个字符
for item in 'hello':
print(item) #只能取出key,然后通过key来去对应的值
dic={'x':444,'y':333,'z':555}
for k in dic:
print(k,dic[k]) #range:范围,1-10,取头不取尾,步数为3;
for i in range(1,10,3):
print(i)
>>1,4,7 #省略起点0,默认步长为1,结果是0-9,十个数
for i in range(10):
print(i)
取出编号并对应老师名:
student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']
for i in range(len(student)):#len(student)=5,即range(0,5),i=0,1,2,3,4
print(i,student[i]) 运行结果:
0 egon
1 虎老师
2 lxxdsb
3 alexdsb
4 wupeiqisb
流程控制if、while、for的更多相关文章
- 第10章 Shell编程(4)_流程控制
5. 流程控制 5.1 if语句 (1)格式: 格式1 格式2 多分支if if [ 条件判断式 ];then #程序 else #程序 fi if [ 条件判断式 ] then #程序 else # ...
- Shell命令和流程控制
Shell命令和流程控制 在shell脚本中可以使用三类命令: 1)Unix 命令: 虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令.这些命令通常是用来进行文件和文字 ...
- PHP基础知识之流程控制的替代语法
PHP 提供了一些流程控制的替代语法,包括 if,while,for,foreach 和 switch. 替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,e ...
- Python黑帽编程2.4 流程控制
Python黑帽编程2.4 流程控制 本节要介绍的是Python编程中和流程控制有关的关键字和相关内容. 2.4.1 if …..else 先上一段代码: #!/usr/bin/python # - ...
- 使用yield进行异步流程控制
现状 目前我们对异步回调的解决方案有这么几种:回调,deferred/promise和事件触发.回调的方式自不必说,需要硬编码调用,而且有可能会出现复杂的嵌套关系,造成"回调黑洞" ...
- [Java入门笔记] Java语言基础(四):流程控制
流程控制指的是在程序运行的过程中控制程序运行走向的方式.主要分为以下几种: 顺序结构 顺序结构,顾名思义,是指程序从上往下逐步顺序执行.中间没有任何的判断和跳转. 分支结构 Java提供两种分支结构: ...
- node基础13:异步流程控制
1.流程控制 因为在node中大部分的api都是异步的,比如说读取文件,如果采用回调函数的形式,很容易造成地狱回调,代码非常不容易进行维护. 因此,为了解决这个问题,有大神写了async这个中间件.极 ...
- Shell入门教程:流程控制(1)命令的结束状态
在Bash Shell中,流程控制命令有2大类:“条件”.“循环”.属于“条件”的有:if.case:属于“循环”的有:for.while.until:命令 select 既属于“条件”,也属于“循环 ...
- Oracle中PL/SQL的执行部分和各种流程控制
Oracle中PL/SQL的执行部分和异常部分 一.PL/SQL的执行部分. 赋值语句. 赋值语句分两种,一种是定义一个变量,然后接收用户的IO赋值:另一种是通过SQL查询结果赋值. 用户赋值举例: ...
- swift_简单值 | 元祖 | 流程控制 | 字符串 | 集合
//: Playground - noun: a place where people can play import Cocoa var str = "Hello, playground& ...
随机推荐
- some settings for spacemacs golang
spacemacs 中的 golang配置 spacemacs 中的 golang layer 已经有很多默认的配置了, 但是都是针对在 GOPATH 下的配置. 如果你的项目不再默认 的 GOPAT ...
- mysql的分组
以下是根据老师的视屏写的总结,要自己实际操作以下. 首先老师一顿操作猛如虎,得到以下的表. 然后进行以下的操作: 发现筛选时报错了,老师的解释实说,分组是因为mysql不知道选择谁而出现报错,因为pa ...
- esp8266 免费wifi强推广告神器(4) 发现当前WIFI下的用户数目,IP,MAC请求http信息 在用户请求跳转后跳转
需求: 1 获取当前连接客户端的HTTP请求各种信息 方法 get http 请求路径 例如 /index.html / /pic.jpg 请求版本 HTTP/1.0 HT ...
- Locust:简介和基本用法
我个人在性能测试工作中,负载生成工具使用的大多都是jmeter,之前学习python时顺带了解过python开源的性能测试框架locust. 这篇博客,简单介绍下locust的使用方法,仅供参考... ...
- .Net Core应用框架Util介绍(六)
前面介绍了Util是如何封装以降低Angular应用的开发成本. 现在把关注点移到服务端,本文将介绍分层架构各构造块及基类,并对不同层次的开发人员应如何进行业务开发提供一些建议. Util分层架构介绍 ...
- Mariadb第一章:介绍及安装--小白博客
mariadb(第一章) 数据库介绍 1.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以 ...
- Python基础知识3-函数、参数及参数解构
函数 函数定义.调用 函数参数 函数参数默认参数 函数参数默认值 可变参数 keyword-only参数 可变参数和参数默认值 函数参数 参数解构 练习: #编写一个函数,能够接受至少2个参数 def ...
- ABP中的拦截器之EntityHistoryInterceptor
今天我们接着之前的系列接着来写另外一种拦截器EntityHistoryInterceptor,这个拦截器到底是做什么的呢?这个从字面上理解是实体历史?这个到底是什么意思?带着这个问题我们来一步步去分析 ...
- AirBnB春招笔试题
试题说明 笔试题只有一道,限时1小时. 模拟一个战争外交游戏,游戏中定义了三种操作: A city1 Hold : 军队A 占领了city1 A city1 Move city2 : 军队A从city ...
- CMS Collector and G1 Collector
Understanding the CMS Collector CMS has three basic operations: CMS collects the young generation (s ...