江西财经大学二学位python第5次实验
# 5.1
def interBMI(height , weight):
BMI = weight/(height * height)
if BMI < 18.5:
result = "偏瘦"
elif 18.5<= BMI < 25:
result = "正常"
elif 25 <= BMI <30:
result = "偏胖"
else :
result = "肥胖"
print("国际:%s" % result )
def domBMI(height , weight):
BMI = weight/(height * height)
if BMI < 18.5:
result = "偏瘦"
elif 18.5<= BMI < 24:
result = "正常"
elif 24 <= BMI <28:
result = "偏胖"
else :
result = "肥胖"
print("国内:%s" % result)
height, weight = eval(input("请输入身高(m)、体重(kg):"))
BMI = weight/(height * height)
print("BMI值为:{:.2f}".format(BMI))
interBMI(height , weight)
domBMI(height , weight)
# 5.2
height , weight = eval(input("请输入身高(m)和体重(kg):"))
bmi = weight / pow(height,2)
print("bmi数值为:{:.2f}".format(bmi))
who, dom = "",""
if bmi < 18.5:
who, dom = "偏瘦","偏瘦"
elif 18.5 <= bmi < 24:
who, dom = "正常","正常"
elif 24 <= bmi < 25:
who, dom = "正常","偏胖"
elif 25 <= bmi < 28:
who, dom = "偏胖","偏胖"
elif 28 <= bmi < 30:
who, dom = "偏胖","肥胖"
else:
who, dom = "肥胖","肥胖"
print("BMI指标为:国际'{0}',国内'{1}'".format(who,dom))
# 6.1
from random import random
from math import sqrt
from time import process_time
DARTS = 1000
hits = 0.0
process_time() #process_time()函数第一次调用时,开始记时,第二次调用返回运行时间
for i in range(1,1+DARTS):
x, y = random(), random()
dist = sqrt(x ** 2 + y ** 2)
if dist <= 1.0:
hits = hits + 1
pi = 4 * (hits/DARTS)
print("pi值是{}.".format(pi))
print("运行时间:{:.5f} s".format(process_time()))
# 4.4
from random import*
Num = randint(0,100)
UsrNum = eval(input("请输入你猜的数字:"))
counter = 1
while(Num != UsrNum):
if(UsrNum>Num):
print("遗憾,太大了")
counter = counter + 1
else:
print("遗憾,太小了")
counter = counter + 1
UsrNum = eval(input("请输入你猜的数字:"))
print("预测了%s次" % counter )
#4.2
str = input("请输入一行字符:")
alpha,num,space,other=0,0,0,0
for i in str:
if i.isalpha():
alpha+=1
elif i.isdigit():
num+=1
elif i.isspace():
space+=1
else:
other+=1
print('英文字符数{},数字字符数{},空格字符数{},其他字符数{}'.format(alpha,num,space,other))