Files
2025PY/day07/03-if语句大练习.py
2025-05-22 16:50:44 +08:00

70 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 1.用户输入两个整数,比较这两个数的大小,并输出较大的数。
# 比较整数int函数转换
# 比较小数float函数转换
# n1 = int(input('请输入第一个整数:'))
# n2 = int(input('请输入第二个整数:'))
# 三种情况(第一个大,第二个大,相等)
# if n1 > n2 :
# print(f'最大的数字是:{n1}')
# elif n1 < n2 :
# print(f'最大的数字是:{n2}')
# else :
# print(f'n1:{n1}和n2:{n2}是相等的')
# 2.用户输入三角形的三边长,判断它是否为等边、等腰或普通三角形
# 三个条件if多分支
# 如果三条边都相等,必须是等边三角形
# 如果三边有任意的两边相等,必须是等腰三角形
# 如果不满足上面的两点,就是普通三角形
# 代码实现
# 第一步:输入三个数字
# n1 = int(input('请输入第一个边的长度:'))
# n2 = int(input('请输入第二个边的长度:'))
# n3 = int(input('请输入第三个边的长度:'))
# 第二步,设置条件
# if n1 == n2 == n3 : # 三边相同
# print(f'你输入的n1={n1},n2={n2},n3={n3}相同是等边三角形')
# elif n1 == n2 or n2 == n3 or n1 == n3 : # 任意两边相同
# print(f'你输入的n1={n1},n2={n2},n3={n3}任意两边相同是等腰三角形')
# else: # 其他情形
# print('你输入的三个数字不相等,是一个普通三角形')
# 3.根据公式(身高-108)* 2 = 体重可以有10斤左右的浮动来观察测试者体重是否标准(身高cm)
# 分析结果:身材是标准的,身材是偏胖的,身材是偏瘦的
# 根据结果得到需要三个条件if的多分支
# 前提
# 输入身高和体重
# 通过公式计算最大体重的分界点和最小体重的分界点
# 条件1体重超过最大体重分界点身材是偏胖的
# 条件2体重比最小体重分界点小身材是偏瘦的
# 条件3体重在最大分界点和最小分界点区间内身材是标准的
# 代码实现
# 输入身高(cm)和体重(斤)
height = int(input('请输入你的身高cm')) # 你的身高
weight = int(input('请输入你的体重斤:')) # 你的体重
# 通过公式计算最大体重的分界点和最小体重的分界点
max_weight = (height-108)* 2 + 10 # 最大体重的分界点
min_weight = (height-108)* 2 - 10 # 最小体重的分界点
# 条件
if weight > max_weight : # 偏胖
print('你太胖了,需要减减肥!')
elif weight < min_weight : # 偏瘦
print('你太瘦了,需要多多加强营养!')
else : # 基于最大和最小之间的,标准身材
print('你是标准身材,请继续保持!')