1、分支结构
(1)单分支结构
(2)二分支结构
(3)多分支结构
条件判断
(4)程序的异常处理
2、实例:身体质量指数BMI
思路一(国内,稍作修改就是国际):
思路二:
1 height,weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]")) 2 bmi = weight / pow(height,2) 3 print("BMI 数值为:{:.2f}".format(bmi)) 4 who, nat = "", "" 5 if bmi < 18.5: 6 who, nat = "偏瘦","偏瘦" 7 elif 18.5 <= bmi < 24: 8 who, nat = "正常","正常" 9 elif 24 <= bmi < 25: 10 who, nat = "正常","偏胖" 11 elif 25 <= bmi < 28: 12 who, nat = "偏胖","偏胖" 13 elif 28 <= bmi < 30: 14 who, nat = "偏胖","肥胖" 15 else: 16 who, nat = "肥胖","偏胖" 17 print("BIM指标为:国际'{0}',国内‘{1}’".format(who,nat)) 18
3、程序的循环结构
字符串遍历循环
列表遍历循环
文件遍历循环
小结
无限循环
循环控制保留字
循环的高级用法
4、random库的使用
5、实例:圆周率的计算
加斜杠后可以写在两行
1 from random import random 2 from time import perf_counter 3 DARTS = 1000*1000 4 hits = 0.0 5 start = perf_counter() 6 for i in range(1,DARTS+1): 7 x,y = random(), random() 8 dist = pow(x**2 + y**2,0.5) 9 if dist <= 1.0: 10 hits = hits + 1 11 pi = 4 * (hits/DARTS) 12 print("圆周率值是:{}".format(pi)) 13 print("运行时间是:{:.5f}s".format(perf_counter()-start))