1.小明身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数:
-
低于18.5:过轻
-
18.5-25:正常
-
25-28:过重
-
28-32:肥胖
-
高于32:严重肥胖
用if-elif
判断并打印结果
hight = float(input("请输入你的身高:(/m)")) weight = float(input("请输入你的体重:(/kg)")) BMI = weight / (hight ** 2) if BMI < 18.5: print("体重过轻") elif 18.5 <= BMI < 25: print("体重正常") elif 25 <= BMI < 28: print("体重过重") elif 28 <= BMI < 32: print("体重肥胖") elif BMI >= 32: print("严重肥胖")
2.输入一个月,判断季节(让用户输入一个月份,判断这个月是哪个季节?假定3到4月是春季,5到8月是夏季,9到10是秋季,11、12、1、2月是冬季)
a = int (input("请输入一个月份:")) if 3 <= a <=4: print("%s月是春季"%a) elif 5 <= a <=8: print("%s月是夏季"%a) elif 9 <= a <=10: print("%s月是秋季"%a) else: print("%s月是冬季"%a)
3.九九乘法表
i = 1 while i <= 9: j = 1 while j <= i: res = i * j if res < 10: #结果小于10打印两个空格 print("%s X %s = %s " %(j,i,(i*j)),end = "") else: print("%s X %s = %s " %(j,i,(i*j)),end = "") j += 1 print() i += 1
4.计算下列多项式之和
sum = 0 for i in range(1, 101): sum = sum + 1 / i print("sum=%f" % sum)
sum = 0 f = 1 for i in range(1, 101): sum = sum + 1 / i * f f = -f print("sum=%f" % sum)
sum = 0 t = 0 for i in range(1, 101): t = t + i sum = sum + 1 / t print("sum=%f" % sum)
5.冒泡排序
arr = [45, 56, 85,7, 6, 58, 42, 14, 65] for i in range(len(arr) - 1): for j in range(len(arr) - 1 - i): if arr[j] >= arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] print(arr)