Python123: 程序的控制结构 (第4周)

1、实例5:身体质量指数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(“BMI指标为:国际’{0}’,国内’{1}’”.format(who, nat))

2、实例6:圆周率的计算

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、整数的加减和

a=0
for i in range(967):
if i%2 == 0:
a -= i
else:
a += i
print(a)

4、三位水仙花数

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次用户名或者密码均有误!退出程序。”)

你可能感兴趣的:(Python基础)