文章目录
一.流程控制分类
1.顺序流程
代码自上而下执行,python默认执行流程
2.选择流程
根据某一步的判断,有选择的去执行相应的逻辑的一种结构
2.1单分支
if 条件表达式:
一条条python语句
一条条python语句
一条条python语句
……
2.2双分支
if 条件表达式:
一条条python语句
一条条python语句
一条条python语句
……
else :
一条条python语句
一条条python语句
一条条python语句
……
2.3多分支
if 条件表达式:
一条条python语句
一条条python语句
一条条python语句
……
elif 条件表达式:
一条条python语句
一条条python语句
一条条python语句
……
elif 条件表达式:
一条条python语句
一条条python语句
一条条python语句
……
elif 条件表达式:
一条条python语句
一条条python语句
一条条python语句
……
else :
一条条python语句
一条条python语句
一条条python语句
……
3.循环流程
while 条件表达式:
一条条python语句
一条条python语句
一条条python语句
……
for …… in 可迭代集合对象:
二.if-else语句
#单分支
print("-----单分支-----")
score=60
if score<=60:
print("成绩不理想")
pass #空语句可用作结束,也可以直接顶格写跳出循环
print("-----语句运行结束-----")
#双分支
print("-----双分支-----")
if score>60:
print("合格")
pass
else:
print("不合格")
pass
print("-----语句运行结束-----")
#多分枝
print("-----多分枝-----")
score=int(input("请输入你的成绩:"))
if score>90:
print("您的成绩是A等级")
elif score>=80:
print("您的成绩是B等级")
elif score>=70:
print("您的成绩是C等级")
elif score>=60:
print("您的成绩是D等级")
else:#选配
print("不及格!")
print("-----语句运行结束-----")
多分支:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KrTFKTyd-1619683383385)(https://i.loli.net/2021/04/22/thOivGFRkx8XsZW.png)]
多分支案例练习——猜拳小游戏
# 0:石头 1:剪刀 2:布
import random#直接导入产生随机数的模块
# 对象:计算机、人
person=int(input("请出拳:【0:石头 1:剪刀 2:布】"))
computer=random.randint(0,2)
print("你:{}---电脑:{}".format(person,computer))
if person==0 and computer==1:
print("厉害了……你赢了")
pass
elif person==1 and computer==2:
print("厉害了……你赢了")
elif person==2 and computer==0:
print("厉害了……你赢了")
elif person==computer:
print('不错 居然平手')
else:
print('哈哈 你输了')
三.if语句的嵌套使用
嵌套if-else使用
#if-else 的嵌套使用
XueFen=int(input("请输入你的学分"))
grade=int(input("请输入你的成绩"))
if XueFen>10:
if grade>=80:
print("恭喜你可以升班了!!!")
pass
else:
print("您的成绩不达标")
pass
pass
else:
print("您的学分不达标")
pass
四.while循环
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ppjT9EYo-1619683383391)(https://i.loli.net/2021/04/22/OGIDrsFkgLt9WnB.png)]
案例 输出1-100
index=1
while index<=100:
print(index)
index+=1
pass
猜拳改进
# 0:石头 1:剪刀 2:布
import random#直接导入产生随机数的模块
# 对象:计算机、人
count=1
while count<=3:
print("第%s局"%count)
person=int(input("请出拳:【0:石头 1:剪刀 2:布】"))
computer=random.randint(0,2)
print("你:{}---电脑:{}".format(person,computer))
if person==0 and computer==1:
print("厉害了……你赢了")
pass
elif person==1 and computer==2:
print("厉害了……你赢了")
elif person==2 and computer==0:
print("厉害了……你赢了")
elif person==computer:
print('不错 居然平手')
else:
print('哈哈 你输了')
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=9
while row>=1:
col=1
while col<=row:
print("%d*%d=%d"%(row,col,row*col),end=" ")
col+=1
pass
print("")
row-=1
pass
注:print函数中end的作用
#默认用换行符连接两行
print("first")
print("second")
#不换行,用空字符串连接两行
print("first",end='')
print("second")
#不换行,用空格连接两行
print("first",end=' ')
print("second")
#不换行,用字符xx连接两行
print("first",end='xx')
print("second")
转载:https://blog.csdn.net/yeziand01/article/details/85940717
打印直角三角形
print中不加end:
row=1
while row<=7:
j=1
while j<=row:
print("*")
j+=1
pass
print()
row+=1
pass
print中加入end:
row=1
while row<=7:
j=1
while j<=row:
print("*",end=' ')
j+=1
pass
print()
row+=1
pass
row=7#先输入第7行数据
while row>=1:
j=1
#内部while循环的作用是row是多少就在一行打印多少个*
while j<=row:
print("*",end=' ')
j+=1
pass
print()
row-=1
pass
打印等腰三角形
row=1
#row控制一共输出5行
while row<=5:
j=1
#控制打印空格的数量
#行数:空格个数 1:4 2:3 3:2 4:1 5:0
while j<=5-row:
print(' ',end='')
j+=1
pass
k=1
# 控制打印*
#行数:*号数 1:1 2:3 3:5 4:7 5:9 打印2*row-1个*
while k<=2*row-1:
print('*',end='')
k+=1
pass
print()
row+=1
思考如果要打印10行应该如何修改?
五.for循环
#字符串类型本身就是一个字符类型的集合
tags='我是中国人'
for item in tags:
print(item)
pass
#range 此函数可以生成一个数据集合列表
#range(起始:结束:步长) 步长不能为0
print(type(range(1,101)))#左边包含右边不包含
sum=0
for data in range(1,101):
sum+=data #求累加和
#print(data,end=' ')
pass
print("sum=%d"%sum)
for data in range(50,201):
if data%2==0:
print("%d是偶数"%data,end=' ')
pass
else:
print("%d是奇数"%data,end=' ')
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
sum=0
for item in range(1,51):
if sum>100:
print('循环执行到%d就退出来了'%item)
break #累加和大于100不在执行
pass
sum+=item
print(sum)
pass
for item in range(1,101):
if item%2==0:
continue
print('在continue的后面的循环体内容不会被执行')
pass
print(item)
pass
for item in 'I love Python':
if item=='e':
break
pass
print(item,end=' ')
for item in 'I love Python':
if item=='e':
continue
pass
print(item,end=' ')
七.for–else与while–else
循环体中只要执行过break就不会执行后面的else
后面的循环体内容不会被执行’)
pass
print(item)
pass
[外链图片转存中...(img-hKf4jCCU-1619683383402)]
for item in ‘I love Python’:
if item==‘e’:
break
pass
print(item,end=’ ')
[外链图片转存中...(img-t6JbPGhJ-1619683383403)]
for item in ‘I love Python’:
if item==‘e’:
continue
pass
print(item,end=’ ')
[外链图片转存中...(img-3w1Z62Be-1619683383404)]
## 七.for--else与while--else
循环体中只要执行过break就不会执行后面的else