9. 流程控制
流程就是计算机执行的顺序,顺序结构,分支结构,循环结构
9.1 分支结构
单向分支,双向分支,多向分支 etc
单向分支:如果判断表达式成立,那么就要执行代码区间,否则不执行。
双向分支:表达式成立,执行真区间,否则执行假区间。
if True:
print()
else:
print()
多向分支: 需要判断多个表达式结果,执行其中符合条件的一个。
score = 90
if score >= 90 and score <=100:
print('hiii')
elif score >= 80 and score >90:
print('next')
elif score >=70 and score <=80:
print('fe')
else:
print()
巢状分支:在分支条件中,嵌套分支
if 表达式a:
if 表达式b:
if 表达式c:
.......
else:
注意:
if True:
pass ;在代码库中专门用于 占位
练习题-十二生肖
要求输入一个四位数年,判断当前年份属于哪一个生肖
子(鼠)、丑(牛)、寅(虎)、卯(兔)、辰(龙)、巳(蛇)、午(马)、未(羊)、申(猴)、酉(鸡)、戌(狗)、亥(猪)
-。
-。
-。
-。
-。
-。
申(猴)、酉(鸡)、戌(狗)、亥(猪)、子(鼠)、丑(牛)、寅(虎)、卯(兔)、辰(龙)、巳(蛇)、午(马) 分别对应0--11,
adict = ['monkey', 'zodiac', 'dog', 'pig', 'rate', 'cow', 'tiger','rabbit', 'dragon', 'snake', 'horse', 'sheep']
%年份取余对应
input()获取用户输入,字符类型!!
9.2 循环结构
-
while 循环:判断当前的条件是否成立
while <expr>:
<statement(s)>
else:
<additional_statement(s)>
num = 1
while num <= 10:
print(num)
num+=1 # 更改变量
目的主要是为了继续进行下一步,并且朝着循环结束的方向发展
-
for 循环:通常用来遍历一个容器类型的数据
for <variable> in <sequence>:
<statements>
else:
<statements>
-
vars = '124234534' for i in vars: print(i) # for i in range(0,10): # range()返回可迭代对象
-
其他流程控制语句
1.break :跳出循环,后面不再执行,结束循环
2.continue :跳过本次循环,继续执行下一次循环
3.pass
n = 0
while n <= 9:
n += 1
if n % 2 == 0:
continue
else:
print(n)
if num == 7:
break
-
特殊语句
exit()
quit()
用于结束python解释器程序的执行,之后代码不会执行。和break类似,但不能混为一谈!!
break和continue是用来控制程序的
无限循环,可以使用 Ctrl+C 来中断循环
9.3 流程控制-练习题
循环输出十行十列'♥'
隔行换色,隔列换色
-。
-。
-。
-。
n = 0
while n < 100:
print('※', end=" ") # 打印到一行!!
if n % 10 == 9:
print()
n += 1
while n < 100:
print('※', end=" ") # 打印到一行!!
n += 1
if n % 10 == 0:
print()
隔列换色
判断当前是奇数还是偶数
n = 0
while n < 100:
if n % 2 == 0:
print('※', end=" ")
else:
print('x', end=" ")
if n % 10 == 9:
print()
n += 1
n = 0
while n < 50:
print('※', end=" ")
n += 1
print("♥", end=" ")
if n % 5 == 0:
print()
隔行换色
n = 0
while n < 100:
#print(n, end=' ')
#**以当前行的行数作为基础,对2取余**
if n //10% 2 == 0:
print('※', end=" ")
else:
print('x', end=" ")
if n % 10 == 9:
print()
n += 1
三角形 菱形
n = 1
while n < 11:
print('※'*n)
n += 1
m = 11
while n < 11 :
print(' '*m, end=' ')
print('※' * n)
n += 1
m -= 1
2. 99乘法表
-。
-。
-。
-。
-。
-。
#第一层循环控制 9行
for x in range(1, 10):
# 内存和负责当前行的列数,第一行,1列,第二行,2列...9行,9列
for y in range(1, x + 1):
print(f'{x}x{y}={x * y}',end=' ')
print()
x = 1
while x < 10:
for y in range(1, x + 1):
print(f'{x}x{y}={x * y}', end=' ')
x += 1
print()
- 反向输出乘法表
x = 9
while x > 0:
for y in range(1, x+1):
print(f'{x}x{y}={x*y}', end=' ')
x -= 1
print()
- range(9,0,-1),倒着循环到1
3. 斐波那契数列
0,1,1,2,3,5,8,13.....
第0项0,则第一项是1,第二项也为1,之后的第三项开始,每一项都是前面两项的和
n = int(input('please input int number n: '))
n1 = 0
n2 = 1
count = 2
if n <= 0:
print("please input positive int number ")
elif n == 1:
print(f'febonaci array is {n1}')
else:
print(f'febonaci array is {n1},{n2}',end=',')
while count < n:
n3 = n1 + n2
print(n3, end=',')
n1, n2 = n2, n3
count += 1
4. 百钱买百鸡
100块钱买100只鸡
公鸡,3元;母鸡,1元;小鸡,0.5元
for x in range(0,34):
for y in range(0,101):
for z in range(0,201):
if x + y + z == 100 and y + 0.5*z + 3*x == 100:
count += 1
print(x,y,z)
print(count)
- 提升性能版
count = 0
for x in range(0, 34):
for y in range(0, 101):
z = 100 - x - y
if (x + y + z) == 100 and (y + 0.5 * z + 3 * x) == 100:
count += 1
print(x, y, z)
print(count)