程序的控制结构

程序的控制结构

分支结构

单分支结构

根据判断条件结果而选择不同向前路径的运行方式

guess = eval(input("请输入你猜测的年龄:"))
if guess == 99:
    print("猜对了")

​ 请输入你猜测的年龄:99
​ 猜对了

二分支结构

根据判断条件结果而选择不同向前路径的运行方式

guess = eval(input())
if guess == 99:
    print("猜对了")
else:
    print("猜错了")
guess = eval(input())
print("猜{}了".format("对" if guess == 99 else "错"))

​ 26
​ 猜错了

多分支结构

score = eval(input())
if score >= 60:
    grade = 'D'
elif score >= 70:
    grade = 'C'
elif score >= 80:
    grade = 'B'
elif score >= 90:
    grade = 'A'
print("输入成绩属于级别{}".format(grade))

条件判断及组合

条件判断

操作符 数学符号 描述
< < 小于
<= 小于等于
>= 大于等于
> > 大于
== = 等于
!= 不等于

条件组合

用于条件组合的三个保留字

操作符及使用 描述
x and y 两个条件x和y的逻辑与
x or y 两个条件x和y的逻辑或
not x 条件x的逻辑非

条件判断及组合

guess = eval(input())
if guess > 99 or guess < 99:
    print("猜错了")
else:
    print("猜对了")

程序的异常处理

异常处理

try:
    num = eval(input("请输入一个整数: "))
    print(num**2)
# 标注异常类型后,仅响应该异常
# 异常类型名字等同于变量
except NameError:
    print("输入不是整数")

"身体质量指数BMI"实例详解

思路方法

  • 难点在于同时输出国际和国内对应的分类
  • 思路1:分别计算并给出国际和国内BMI分类
  • 思路2:混合计算并给出国际和国内BMI分类
分类 国际BMI值(kg/m2)(kg/m2) 国内BMI值(kg/m2)(kg/m2)
偏瘦 <18.5 <18.5
正常 18.5 ~ 25 18.5 ~ 24
偏胖 25 ~ 30 24 ~ 28
肥胖 ≥30 ≥28

国际版

# CalBMIv1.py
height, weight = eval(input("请输入身高(米)和体重\(公斤)[逗号隔开]: "))
bmi = weight / pow(height, 2)
print("BMI 数值为:{:.2f}".format(bmi))

who = ""
if bmi < 18.5:
    who = "偏瘦"
elif 18.5 <= bmi < 25:
    who = "正常"
elif 25 <= bmi < 30:
    who = "偏胖"
print("BMI 指标为:国际'{0}'".format(who))

​ 请输入身高(米)和体重(公斤)[逗号隔开]: 1.8,70
​ BMI 数值为:21.60
​ BMI 指标为:国际'正常'

国内版

# CalBMIv2.py
height, weight = eval(input("请输入身高(米)和体重\(公斤)[逗号隔开]: "))
bmi = weight / pow(height, 2)
print("BMI 数值为:{:.2f}".format(bmi))

nat = ""
if bmi < 18.5:
    nat = "偏瘦"
elif 18.5 <= bmi < 24:
    nat = "正常"
elif 25 <= bmi < 38:
    nat = "偏胖"
print("BMI 指标为:国内'{0}'".format(nat))

​ 请输入身高(米)和体重(公斤)[逗号隔开]: 1.8,70
​ BMI 数值为:21.60
​ BMI 指标为:国内'正常'

综合版

# CalBMIv3.py
height, weight = eval(input("请输入身高(米)和体重\(公斤)[逗号隔开]: "))
bmi = weight / pow(height, 2)
print("BMI 数值为:{:.2f}".format(bmi))
who, nat = "", ""
if bmi < 18.5:
    who, nat = "偏瘦", "偏瘦"
elif 18.5 <= bmi < 24:
    who, nat = "正常", "正常"
elif 24 <= bmi < 25:
    who, nat = "正常", "偏胖"
elif 25 <= bmi < 28:
    who, nat = "偏胖", "偏胖"
elif 28 <= bmi < 30:
    who, nat = "偏胖", "肥胖"
else:
    who, nat = "肥胖", "肥胖"
print("BMI 指标为:国际'{0}', 国内'{1}'".format(who, nat))

​ 请输入身高(米)和体重(公斤)[逗号隔开]: 1.8,70
​ BMI 数值为:21.60
​ BMI 指标为:国际'正常', 国内'正常'

程序的循环结构

遍历循环

遍历某个结构形成的循环运行方式

  • 从遍历结构中逐一提取元素,放在循环变量中
  • 由保留字forin组成,完整遍历所有元素后结束
  • 每次循环,所获得元素放入循环变量,并执行
for i in range(5):
    print('hello:', i)

​ hello: 0
​ hello: 1
​ hello: 2
​ hello: 3
​ hello: 4

计数循环(特定次)

  • 遍历由range()函数产生的数字序列,产生循环
for i in range(1, 6, 2):
    print('hello:', i)

​ hello: 1
​ hello: 3
​ hello: 5

字符串遍历循环

  • 遍历字符串每个字符,产生循环
for c in 'python':
    print(c, end=',')

​ p,y,t,h,o,n,

列表遍历循环

  • 遍历其每个元素,产生循环
for item in [123, "PY", 456]:
    print(item, end=" ")

​ 123 PY 456

文件遍历循环

f = open("C:\\Users\青柠\Desktop\\1.txt") # 打开文件所在目录
f1 = f.read() # 读取文件
for i in f1:
    print(i,end="") # 打印文件

无限循环

  • 反复执行语句块,直到条件不满足时结束
# 死循环, (CTRL + C 退出执行) 
a = 3
while a > 0:
    a = a + 1
    print(a)

循环控制保留字

break和continue

  • break跳出并结束当前整个循环,执行循环后的语句
  • continue结束当次循环,继续执行后续次数循环
  • break和continue可以与for和while循环搭配使用

for

for c in "PYTHON":
    if c == 'T':
        continue
    print(c, end=',')

for c in "PYTHON":
    if c == 'T':
        break
    print(c, end=',')

​ P,Y,H,O,N,

​ P,Y,

while

  • break仅跳出当前最内层循环
s = "PYTHON"
while s != "":
    for c in s:
        print(c, end=',')
    s = s[:-1]

s = "PYTHON"
while s != "":
    for c in s:
        if c == 'T':
            break
        print(c, end=',')
    s = s[:-1]

​ P,Y,T,H,O,N,P,Y,T,H,O,P,Y,T,H,P,Y,T,P,Y,P,

​ P,Y,P,Y,P,Y,P,Y,P,Y,P,

posted @ 2019-07-17 17:27  SweetJack  阅读(373)  评论(0编辑  收藏  举报