学习内容
底四周的学习内容包括分支结构、循环结构和random库,里面作了两个实例:BMI计算和圆周率的计算。我认为学习的重点主要在理解结构的使用方式上面。
分支结构
程序有单分支、二分支、多分支三种分支结构。多用if、elif、else等函数。此处还学习了条件判断操作符,与数学符号相差不大。
操作符号 | 数学符号 |
<\<=\>=\>\==\!= | <\<=\>=\>\==\≠ |
条件组合 | 描述 |
and、or、not | 很常见的与、或、非 |
课程中还讲述了程序异常处理的方法,算是对学习的一个补充,此处需要在实践过程中进行练习,还没有进行系统的梳理,暂且按下不表。
实例:BMI计算
1.
#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 = "偏胖"
else:
who = "肥胖"
print("BMI 指标为:国际'{0}'".format(who))
2.
#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 24 <= bmi < 28:
nat = "偏胖"
else:
nat = "肥胖"
print("BMI 指标为:国内'{0}'".format(nat))
3.
#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))
循环结构
循环分为遍历循环和无限循环,了解循环还需要学习循环的保留字,课上也给出了循环的高级用法。
遍历循环:
for <循环变量> in <遍历结构>:
<语句块>
此处给出了计数循环的函数range:
for i in range ():
print(i)
字符串遍历循环:
for c in s :
<语句> #s 字符串,遍历字符串每个字符,产生循环,将s换成ls则ls是一个列表;fi是文件。
无限循环:
while <条件> :
<语句块>
循环控制保留字:break 跳出并结束循环,执行循环后的语句。continue 结束当次循环,继续执行后续循环次数。一般与for、while搭配使用。
高级用法(其实不高级):
在for、while循环中加入了else。
random库的使用
random主要用于生成随机数。生成的是采用梅森旋转算法生成的(伪)随机序列中的元素。一共包括两类八个函数:
基本随机函数 | seed()\、random() |
扩展随机函数 | randint(a,b)生成a到b随机整数、getradbits(k)生成k比特长的随机整数、uniform(a,b)随机小数、randrange(m,n[,k])生成m,n之间k步长的随机整数、choice(seq)类表中随机挑选、shuffle(seq)列表中随机排列后返回打乱序列 |
实例:圆周率计算
公式方法:
#CalPiV1.py
pi = 0
N = 100
for k in range(N):
pi += 1/pow(16,k)*( \
4/(8*k+1) - 2/(8*k+4) - \
1/(8*k+5) - 1/(8*k+6) )
print("圆周率值是: {}".format(pi))
蒙特卡洛方法:
#CalPiV2.py
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))
练习题
1.
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 30>BMI>=28:
who,nat ="偏胖","肥胖"
elif BMI>=30:
who,nat ="肥胖","肥胖"
print("BMI指标为:国际'{0}',国内'{0}'".format(who,nat))
2.
from random import random, seed
DARTS = eval(input())
seed(123)
hits = 0.0
for i in range(DARTS):
x, y = random(), random()
dist = pow(x ** 2 + y ** 2, 0.5)
if dist <= 1.0:
hits = hits + 1
pi = 4 * (hits/DARTS)
print("{:.6f}".format(pi))
3.
import math
t=1
u=0
for i in range(1,966):
u=u+t*(pow(-1,t))
if t==966:
break
else:
t = t + 1
print("{}".format(u))
4.
import math
s = ""
for i in range(100,1000):
t=str(i)
if pow(eval(t[0]), 3)+pow(eval(t[1]), 3)+pow(eval(t[2]), 3) == i:
s += "{},".format(i)
print(s[:-1])
5.
count = 0
while count < 3:
name = input()
password = input()
if name == 'Kate'and password == '666666':
print("登录成功!")
break
else:
count += 1
if count == 3:
print("3次用户名或者密码均有误!退出程序。")