1、分支结构
(1)单分支结构
(2)二分支结构
(3)多分支结构
条件判断
(4)程序的异常处理
2、实例:身体质量指数BMI
思路一(国内,稍作修改就是国际):
思路二:
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("BIM指标为:国际'{0}',国内‘{1}’".format(who,nat))
3、程序的循环结构
字符串遍历循环
列表遍历循环
文件遍历循环
小结
无限循环
循环控制保留字
循环的高级用法
4、random库的使用
5、实例:圆周率的计算
加斜杠后可以写在两行
from random import random
from time import perf_counter
DARTS = 1000*1000
hits = 0.0
start = perf_counter()
for i in range(1,DARTS+1):
x,y = random(), random()
dist = pow(x**2 + y**2,0.5)
if dist <= 1.0:
hits = hits + 1
pi = 4 * (hits/DARTS)
print("圆周率值是:{}".format(pi))
print("运行时间是:{:.5f}s".format(perf_counter()-start))