Day 2 python 流程控制

Day 2

流程控制

流程:计算机执行代码的顺序

流程控制:对代码执行顺序进行有效的管理

流程控制分类

  1. 顺序流程——自上而下的执行结构(python默认的流程)

  2. 选择/分支流程——根据某一步的判断,有选择的执行相应逻辑的一种结构

    • 单分支

      ​ if 条件表达式:

      ​ 代码

    • 双分支

      ​ if 条件表达式:

      ​ 代码

      ​ else:

      ​ 代码

    • 多分支

      ​ if 条件表达式:

      ​ 代码

      ​ elif 条件表达式:

      ​ 代码

      ​ elif 条件表达式:

      ​ 代码

      ​ …

      ​ else:

      ​ 代码

      条件表达式——比较/逻辑/复合运算符

  3. 循环流程——在满足一定条件下,一直重复执行某段代码的逻辑

    • while 条件表达式 :

      ​ 代码

    • for…in 可迭代集合对象:

      ​ 代码

选择流程

单分支

score=60
if score<=60:          #满足条件 进行打印
    print("成绩不理想")
    pass    #空语句
print("运行结束")

成绩不理想
运行结束

pass——空语句 一般用做占位语句 为保持程序结构的完整性

双分支

特征:必会执行其中之一

if score>=60:
    print("及格")
    pass
else:
    print("不及格")
    pass

多分支

特征:
1.只要满足其中之一,就会退出本层if【必会执行其中之一】
2.至少两种情况可以选择
3.elif 后面必须跟条件表达式

if score>90:
    print('A')
    pass
elif score>=80:
    print('B')
    pass
elif score>=70:
    print('C')
    pass
elif score>=60:
    print('D')
    pass
else:    #选配
    print('不及格')
print('运行结束')
多分支练习——猜拳机
#多分支练习——猜拳机
#0:石头 1:剪刀 2:布
import random           #导入随机数模块
#计算机 人
person=int(input('请出拳[0:石头 1:剪刀 2:布]'))
computer=random.randint(0,20)
if person==0 and computer==1:      #多条件
    print('person win!')
    pass
elif person==1 and computer==2:
    print('person win!')
    pass
elif person==2 and computer==0:      #多条件
    print('person win!')
    pass
elif person==computer:
    print('平手')
    pass
else:
    print('person lose!')

嵌套

#嵌套
xuefen=int(input('请输入学分 '))
if xuefen>10:
    gread = int(input('请输入成绩 '))
    if gread>=80:
        print('可以升班')
        pass
    else:
        print('成绩不达标')
        pass
    pass
else:
    print('表现太差')

循环流程

循环目的:为了将相似代码操作更加简洁,代码复用

while

while语法结构

while 条件表达式:

​ 代码

while语法特点

  1. 有初始值
  2. 条件表达式
  3. 变量的自增自减,否则造成死循环

使用条件

循环次数不确定,依靠循环条件结束

# 案例1 输出1-100的数据
index=1
while index <=100:
    print(index)
    index+=1
    pass
#多分支/while练习——猜拳机
#0:石头 1:剪刀 2:布
import random           #导入随机数模块
#计算机 人

count=1         #局数变量
while count<=10:     #进行几句
    person = int(input('请出拳[0:石头 1:剪刀 2:布]'))
    computer = random.randint(0, 20)
    if person == 0 and computer == 1:  # 多条件
        print('person win!')
        pass
    elif person == 1 and computer == 2:
        print('person win!')
        pass
    elif person == 2 and computer == 0:  # 多条件
        print('person win!')
        pass
    elif person == computer:
        print('平手')
        pass
    else:
        print('person lose!')
    count+=1

#打印九九乘法表
row=1                    #行变量
while row<=9:
    col=1                  #列变量
    while col<=row:
        print("%d*%d=%d"%(row,col,row*col),end=" ")
        col+=1
        pass
    print(" ")
    row+=1
    pass
#打印直角三角形
row=1
while row<=7:
    j=1
    while j<=row:
        print('*',end=' ')      #空格间隔
        j+=1
        pass
    print()                 #换行
    row+=1
    pass

#打印等腰三角形
row=1
while row<=5:
    j=1
    while j<=5-row:                  #打印空格
        print(' ',end='')            #end=''——不换行
        j+=1
        pass
    k=1
    while k<=2*row-1:                #打印*
        print('*', end='')
        k+= 1
        pass
    print()
    row+=1

for

for循环格式:

for 临时变量 in 字符串、列表等:

​ 代码

for语法特点:

遍历操作,依次取集合容器中每个值

tags='我是Deer'
for item in tags:      #item为自己定义的变量用来存放取出数据
    print(item)
    pass



D
e
e
r

range(起始,结束,步长)——用于生成一个数据集合列表 结束为左闭右开区间

#计算1到100的和
sum=0
for data in  range(1,101):
    sum+=data
    pass
print(sum)

#打印50到101
for data in range(50,101):
    if data%2==0:
        print('%d是偶数'%data)
        pass
    else:
        print('%d是奇数'%data)
        pass

#for循环实现九九乘法表
for i in range(1,10):
    for j in range(1,i+1):
        print("%d*%d=%d"%(i,j,i*j),end=' ')
        pass
    print()
    pass

break&continue

break——中断,结束本层循环

continue——结束本层循环,继续下次循环

只能用在循环当中

break案例

sum=0
for item in range(1,51):
    if sum>100:
        print("循环执行到%d结束"%item)
        break           #退出
        pass
    sum+=item
    pass
print(sum)

循环执行到15结束
105

continue案例

sum=0
for item in range(1,100):
    if item%2==0:
        continue               #跳出if
        print("会执行嘛?")
        pass
    print(item)
    pass


for——else&while——else

#for——else
for item in range(1,11):
    print(item,end=' ')
    if item>=5:
        break
        pass
    pass
else:
    print('上面出现了break,则else不会被执行')
#for——else
account='www'
pwd='123'
for i in range(3):
    zh=input('输入账号')
    pd=input('输入密码')
    if account==zh and pwd==pd:
        print('登陆成功!')
        break
        pass
    pass
else:
    print('已锁定')
#while——else
index=1
while index<=5:
    print(index)
    index+=1
    pass
else:
    print('是否执行?')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值