**
python经典例题1**
1、BMI指数是值体重(kg)除以身高(m)的平方,请根据下列BMI指数判断小明(身高1.75m,体重80.3kg)的类型。
weight = float(input("请输入体重(kg):"))
height = float(input('请输入身高(m):'))
BMI = weight / height ** 2
if 0 < BMI < 18.5:
value = '过轻'
elif 18.5 <= BMI < 25:
value = '正常'
elif 25 <= BMI < 28:
value = "过重"
elif 28 <= BMI < 32:
value = '肥胖'
else:
value = '严重肥胖'
print('BMI = %s' % value)
2、九九乘法表
使用while循环
row = 1
while row <= 9:
column = 1
while column <= row:
product = row * column
print('%d * %d = %-5d' % (row, column, product), end='')
column += 1
print()
row += 1
使用for循环
for row in range(1, 10):
for column in range(1, row + 1):
product = row * column
print('%d * %d = %-3d' % (row, column, product), end=' ')
print()
3、利用下面公式计算 e 的值,要求计算到 n=20,输出小数点后 6 位。
# 使用while循环
# denominator = 1
# n = 1
# e = 1
# while n <= 20:
# e += 1 / denominator
# n += 1
# denominator *= n
# print("e={}".format(e))
# 使用for循环
denominator = 1
e = 1
for n in range(1, 21):
denominator *= n
e += 1.0 / denominator
print('e = {}'.format(e))
4、成绩判定问题
根据输入的分数,判断该学生的分数属于哪个类型。其中分数划档为:
89-100(不包含 89,包含 100)——优秀;
70-89(不包含 70,包含 89)——良;
60-70(包含 60 且包含 70)——一般;
小于 60(不包含 60)——不及格。
scores = int(input("请输入0——100之间的成绩:"))
if 0 < scores <= 100:
if 60 <= scores < 70:
grade = '一般'
elif 70 <= scores <= 89:
grade = '良'
elif 90 <= scores <= 100:
grade = '优秀'
else:
grade = '不及格'
print("等级为:", grade)
else:
print("输入错误")
5、
for chicken in range(1, 65):
for rabbit in range(1, 65):
if 2 * chicken + 4 * rabbit == 64 and chicken >= 14:
print("鸡的数量为 %d 只,兔的数量为 %d 只!" % (chicken, rabbit))
6、求公倍数问题
请输出 1-100 之间能被 5 和 7 整除的数并输出
print('1——100之间能被5和7整除的数有:')
for i in range(1, 101):
if i % 5 == 0 and i % 7 == 0:
print(i)
7、 地铁购票规定
购买地铁车票的规定如下:乘 1~4 站,3 元/位;乘 5 ~9 站,4 元/位;乘 9 站以上,5 元/
位。输入人数、站数,输出应付款。
site = int(input("请输入站数:"))
people = int(input('请输入人数:'))
if site > 0:
if 1 <= site <= 4:
money = 3 * people
elif 5 <= site <= 9:
money = 4 * people
else:
money = 5 * people
print('应付款:', money)
else:
print("输入错误!!!")
8、分数转换成等级制度
如果输入的成绩在 90 分以上(含 90 分)输出 A;80 分-90 分(不含 90 分)输出 B;
70 分-80 分(不含 80 分)输出 C;60 分-70 分(不含 70 分)输出 D;60 分以下输出 E。
score = float(input('请输入成绩:'))
if 0 < score <= 100:
if 60 <= score < 70:
grade = 'D'
elif 70 <= score < 80:
grade = 'C'
elif 80 <= score < 90:
grade = 'B'
elif 90 <= score <= 100:
grade = 'A'
else:
grade = 'E'
print('对应的等级是:', grade)
else:
print("输入错误!!!")
9、象限问题
已知点的坐标(x,y),判断其所在的象限
x = int(input('请输入x的坐标:'))
print('————————————————')
y = int(input('请输入y的坐标:'))
if x == 0 and y == 0:
quadrant = '原点'
elif x == 0:
quadrant = 'y轴'
elif y == 0:
quadrant = 'x轴'
elif x > 0 and y > 0:
quadrant = '一象限'
elif x < 0 and y > 0:
quadrant = '二象限'
elif x < 0 and y < 0:
quadrant = '三象限'
else:
quadrant = '四象限'
print('象限为:', quadrant)
10、 身份验证问题
当用户名为 admin,密码为 root 匹配的时候,才能进入系统,当用户名正确且密
码错误的时候提示密码错误;当用户名错误的时候提示用户名错误;当用户名和密码
正确的时候提示身份验证成功!
user = input('请输入用户名:')
address = input('请输入密码:')
if user == 'admin' and address != 'root':
print('密码错误')
elif user != 'admin' and address == 'root':
print('用户名错误')
elif user != 'admin' and address != 'root':
print('用户名和密码都错误')
else:
print("身份验证成功!!!")
———————————结束—————————————



