03 python开发之流程控制

3 流程控制

3.1 流程判断之if判断

3.1.1 代码块

  • 代码块指同一级别的代码在python中使用相同缩进的空格数

  • 顶级代码块无任何缩进,其余代码都是在原有基础上缩进4个空格来标识同一级别的代码块

  • 同一级别的代码块会按照自上而下的顺序依次执行

3.1.2 if判断基础语法

# 基础语法
if 条件1:
代码1
代码2
代码3
elif 条件2:
...
...
else:
... # 单分支
if 条件:
代码1
代码2
代码3 # 双分支
if 条件1:
代码1
代码2
代码3
elif 条件2:
...
...

if 条件1:
代码1
代码2
代码3
else:
... # 多分支
if 条件1:
代码1
代码2
代码3
elif 条件2:
...
...
else:
...

3.1.3 案例

age =22
height=170
weight=100
gendel="female"
print('我是顶级代码...')
if age > 16 and age < 26 and height >160 and gendel == "female":
print("开始表白。。。。") print('我是顶级代码....')
age_of_girl = input('请输入年龄:')
height = input('请输入身高:')
weight = input('请输入体重:')
is_pretty = input('请给自己颜值打分:') if 18 <= int(age_of_girl) <= 22 and int(height) > 170 and int(weight) < 100 and int(is_pretty) >=80:
print('Get到WeChat')
else:
print('游泳健身了解一下?')
score = input('请输入你的成绩:')
score = int(score)
if score >= 90:
print('优秀!')
elif score >= 80:
print('良好!')
elif score >= 70:
print('普通!')
else:
print('很差!')

3.1.4 if判断嵌套

age =18
height=170
weight=100
gendel="female"
is_ok=True
print('我是顶级代码...')
if 16 < age < 26 and height >160 and gendel =='female':
print("开始表白。。。。")
if is_ok: # 如果is_ok为真则打印
print("在一起。。。。")
else: # 否则打印我们不合适
print("我们不合适。。。")
else:
print("阿姨好。。。。")
print('我是顶级代码....')

3.2 流程判断之while循环

3.2.1 循环基础知识

  • 循环就是重复做某件事

  • 循环是为了让计算机像人一样去重复做某件事

  • while循环也可称为条件循环

3.2.2 while循环基础语法

while 条件:
代码1
代码2
代码3 # 基本使用
count = 0
while count < 6:
print(count)
count += 1

3.2.3 结束while循环的两种方式

  • 条件改为假(必须等到下一次循环判断条件时循环才会结束)
tag = True
while tag:
username = input("username:")
password = input("password:") if username == "ccc" and password == "123":
print('login successfully')
tag = False
else:
print('username or password error')
  • break干掉本层循环,不会进行下一次循环
while tag:
username = input("username:")
password = input("password:") if username == "ccc" and password == "123":
print('login successfully')
break
else:
print('username or password error')

3.2.4 循环嵌套

tag = True
while tag:
while tag:
while tag:
tag = False
while 条件1:
while 条件2:
while 条件3:
break
break
break

3.2.5 while+continue

  • 终止本次循环,直接进入下一次
count = 0
while count < 6:
if count == 3:
count += 1
continue
print(count)
count += 1 # 0 1 2 4 5
continue下面的代码都不会执行,因此continue下面不要写代码!!!切记!!!

3.2.6 while+else

  • 情况一:while循环非正常结束,else不执行
count = 0
while count < 6:
if count == 3:
break
print(count)
count += 1
else:
print('='*5) # 0 1 2
  • 情况二:while正常结束,执行else
count = 0
while count < 6:
if count == 3:
continue
print(count)
count += 1
else:
print('='*10) # 0 1 2 4 5
# ==========

3.2.7 死循环

  • 表达式永远为真的循环称为死循环
count = 0
while count < 4:
print(count) while True:
print('ok') while 1:
print('ok')

3.2.8 案例

username = "ccc"
password = "123" print('顶级代码....')
while True:
inp_name=input("请输入用户名:")
inp_pwd=input("请输入密码:") if inp_name == username and inp_pwd == password:
print('login successful')
break
else:
print('username or password error')
print("====end====")
username = "ccc"
password = "123"
tag = True
print('顶级代码....')
while True:
inp_name = input("请输入用户名:")
inp_pwd = input("请输入密码:") if inp_name == username and inp_pwd == password:
print('login successful')
tag = False
else:
print('username or password error')
print("====end====")
username = "ccc"
password = "123"
count = 0
tag = True
while tag:
inp_user = input("请输入用户名:").strip()
inp_pwd = input("请输入密码:").strip() if inp_user == username and inp_pwd == password:
print("登录成功")
while tag:
print("""
0:退出
1:转账
2:取款
3:查询余额
""")
inp_order = input("请输入命令:").strip()
if inp_order == "0":
print("退出")
tag=False
elif inp_order == "1":
print("转账")
elif inp_order == "2":
print("取款")
elif inp_order == "3":
print("查询余额")
else:
print("输入命令有误")
tag = False
else:
print("用户或密码错误")
count += 1 if count == 3:
print("输入次数已超限制")
tag = False

3.3 流程判断之for循环

3.3.1 for循环基础

  • 对于循环取值while并不擅长,python有专门循环取值的操作:for循环

  • for循环也称为迭代循环

  • for循环与while循环的相同之处在于都是循环,都可以用来做重复的事情

  • for循环与while循环的不同之处在于while循环常用来循环执行某段代码,for循环用来循环取值

  • while循环的次数取决于条件何时为假

  • for循环的次数取决于in后数据包含的值得个数

3.3.2 for循环基本用法

  • 遍历值
info = [["name", "egon"], ["age", 18], ["gender", "male"]]
for x, y in info:
print(x, y) # name egon
# age 18
# gender male
names=["egon","Lxx_dsb","xc","zhoujielun"]
for i in names: # 有几个值就循环几次,4次
print(i) # egon
# Lxx_dsb
# xc
# zhoujielun
d={"k1":111,"k2":222,"k3":333}
for i in d:
print(i) # 从字典里取出的默认是key:k1 k2 k3
print(i, d[i]) # 通过key取value:k1 111 k2 222 k3 333

3.3.3 for+range()

  • range产生一个数字序列,顾头不顾尾

    range(起始位置,结束位置,步长)起始位置默认0,步长默认1

  • 用途:for+range()可以按照索引编列列表

    ​ 1、用来重复n次此段代码

    ​ 2、range可以产生数字序列,数字对应的是列表的索引

range(10)         #等同于range(0, 10)
range(10, 1, -1) # 从大到小必须指定步长 for i in range(5):
print(i)
# 0 1 2 3 4
# 在python2中
>>> range(5), type(range(5))
([0, 1, 2, 3, 4], <type 'list'>) # 在python3中
>>> range(5), type(range(5))
(range(0, 5), <class 'range'>)
l = [111, 222, 333, 444, 555]
i = len(l) - 1
while i > -1:
print(l[i])
i -= 1
# 555 444 333 222 111

l = [111, 222, 333, 444, 555]
for i in range(len(l) - 1, -1, -1):
print(l[i])
# 555 444 333 222 111

3.3.4 for+enumerate

  • 以key:value输出
l = [111, 222, 333, 444, 555]
for i in enumerate(l):
print(i)
# (0, 111)
# (1, 222)
# (2, 333)
# (3, 444)
# (4, 555)
l=[111,222,333,444,555]
for i,y in enumerate(l):
print("index: %s value: %s" % (i,y))
# index: 0 value: 111
# index: 1 value: 222
# index: 2 value: 333
# index: 3 value: 444
# index: 4 value: 555

3.3.5 for+break

for i in range(3):
username=input('username>>: ')
password=input('password>>: ')
if username == 'egon' and password == '123':
break
else:
print('用户名或密码错误')

3.3.6 for+continue

for i in range(5):	 # 0 1 2 3 4
if i == 2:
continue
print(i) # 0 1 3 4

3.3.7 for+else

  • 循环正常结束才会执行,否则不执行
for i in range(5):
if i == 3:
break
print(i)
else:
print("---------") # 不执行
for i in range(5):
if i == 3:
continue
print(i)
else:
print("--------") # 执行

3.3.8 for循环嵌套

for i in range(3):
print("--------loop1------> %s" % i)
for j in range(5):
print("loop2--------> %s" % j) # --------loop1------> 0
# loop2--------> 0
# loop2--------> 1
# loop2--------> 2
# loop2--------> 3
# loop2--------> 4
# --------loop1------> 1
# loop2--------> 0
# loop2--------> 1
# loop2--------> 2
# loop2--------> 3
# loop2--------> 4
# --------loop1------> 2
# loop2--------> 0
# loop2--------> 1
# loop2--------> 2
# loop2--------> 3
# loop2--------> 4

03python开发之流程控制的更多相关文章

  1. iOS开发Swift篇—(六)流程控制

    iOS开发Swift篇—(六)流程控制 一.swift中的流程控制 Swift支持的流程结构如下: 循环结构:for.for-in.while.do-while 选择结构:if.switch 注意:这 ...

  2. 李洪强iOS开发Swift篇—06_流程控制

    李洪强iOS开发Swift篇—06_流程控制 一.swift中的流程控制 Swift支持的流程结构如下: 循环结构:for.for-in.while.do-while 选择结构:if.switch 注 ...

  3. python全栈开发-Day2 布尔、流程控制、循环

    python全栈开发-Day2 布尔 流程控制 循环   一.布尔 1.概述 #布尔值,一个True一个False #计算机俗称电脑,即我们编写程序让计算机运行时,应该是让计算机无限接近人脑,或者说人 ...

  4. Python全栈开发之---输入输出与流程控制

    Python简介 python是吉多·范罗苏姆发明的一种面向对象的脚本语言,可能有些人不知道面向对象和脚本具体是什么意思,但是对于一个初学者来说,现在并不需要明白.大家都知道,当下全栈工程师的概念很火 ...

  5. python 全栈开发,Day50(Javascript简介,第一个JavaScript代码,数据类型,运算符,数据类型转换,流程控制,百度换肤,显示隐藏)

    一.Javascript简介 Web前端有三层: HTML:从语义的角度,描述页面结构 CSS:从审美的角度,描述样式(美化页面) JavaScript:从交互的角度,描述行为(提升用户体验) Jav ...

  6. Python自动化开发 - 流程控制

    一.拾遗主题 1.变量 理解变量在计算机内存中的表示 >>> a = "ABC" Python解释器干了两件事情: 在内存中创建了一个'ABC'的字符串: 在内存 ...

  7. PHP移动互联网开发笔记(3)——运算符与流程控制

    一.PHP的运算符 PHP中有丰富的运算符集,它们中大部分直接来自于C语言.按照不同功能区分,运算符可以分为:算术运算符.字符串运算符.赋值运算符.位运算符.条件运算符,以及逻辑运算符等.当各种运算符 ...

  8. python 之 前端开发( JavaScript变量、数据类型、内置对象、运算符、流程控制、函数)

    11.4 JavaScript 11.41 变量 1.声明变量的语法 // 1. 先声明后定义 var name; // 声明变量时无需指定类型,变量name可以接受任意类型 name= " ...

  9. day04-Python运维开发基础(位运算、代码块、流程控制)

    # (7)位运算符: & | ^ << >> ~ var1 = 19 var2 = 15 # & 按位与 res = var1 & var2 " ...

随机推荐

  1. Luogu-2480 古代猪文

    我们首先来概括一下题意,其实就是给定 \(n,g\),求: \[g^{\sum_{k\nmid n} C_n^{\frac{n}{k}}}\operatorname{mod} 999911659 \] ...

  2. 通过IIS部署,将图片或者视频等文件用http协议网址访问

    打开IIS管理器 又键点击添加网站 然后到这个界面 文件夹里有这些图片,随便用的一些图片 然后我这里用的是局域网测试,所以IP就是wifi的IP地址,如果是服务器的话,直接选服务器本身的IP地址就行了 ...

  3. C#Socket通讯(2)

    前言 前面已经把游戏的服务端UI搭起来来了,现在需要实现的就是编写服务端控制器与客户端的代码,实现服务端与客户端的数据传输,并将传输情况显示在服务端的UI上 服务端控制器完整代码 private st ...

  4. VMware Workstatition启动虚拟机电脑蓝屏

    电脑出了点问题,重装了系统,结果安装VMware之后,一启动虚拟机电脑就蓝屏重启. 系统是win10 19041 开始用的原来下载的vmware15.0,创建虚拟机蓝屏,重启之后可以创建.创建完以后启 ...

  5. 3. Distributional Reinforcement Learning with Quantile Regression

    C51算法理论上用Wasserstein度量衡量两个累积分布函数间的距离证明了价值分布的可行性,但在实际算法中用KL散度对离散支持的概率进行拟合,不能作用于累积分布函数,不能保证Bellman更新收敛 ...

  6. B. Nauuo and Circle 解析(思維、DP)

    Codeforce 1172 B. Nauuo and Circle 解析(思維.DP) 今天我們來看看CF1172B 題目連結 題目 略,請直接看原題 前言 第一個該觀察的事情一直想不到,看了解答也 ...

  7. iOS 14 egret 游戏卡顿问题分析和部分解决办法

    现象 总体而言,iOS 14 渲染性能变差,可以从以下三个测试看出. 测试1:简单demo,使用egret引擎显示3000个图(都是同一个100*100 png 纹理),逐帧做旋转.(博客园视频播放可 ...

  8. 深度对比Apache CarbonData、Hudi和Open Delta三大开源数据湖方案

    摘要:今天我们就来解构数据湖的核心需求,同时深度对比Apache CarbonData.Hudi和Open Delta三大解决方案,帮助用户更好地针对自身场景来做数据湖方案选型. 背景 我们已经看到, ...

  9. 【论文阅读】An Anchor-Free Region Proposal Network for Faster R-CNN based Text Detection Approaches

    懒得转成文字再写一遍了,直接把做过的PPT放出来吧. 论文连接:https://link.zhihu.com/?target=https%3A//arxiv.org/pdf/1804.09003v1. ...

  10. STM32入门系列-STM32时钟系统,时钟初始化配置函数

    在前面推文的介绍中,我们知道STM32系统复位后首先进入SystemInit函数进行时钟的设置,然后进入主函数main.那么我们就来看下SystemInit()函数到底做了哪些操作,首先打开我们前面使 ...