1 """================ if判断 begin ====================== 2 3 # 输入一个年龄,判断是成年人还是小孩 4 age = input(‘请输入年龄‘) 5 if int(age)>=18: 6 print(‘成年人‘) 7 exit() 8 print(‘小孩‘) 9 10 # 输入一个数字,判断是奇数还是偶数 11 num = input(‘请输入一个数字‘) 12 if int(num) % 2 == 0: 13 print(‘偶数‘) 14 exit() 15 print(‘奇数‘) 16 17 # 输入一个月份,判断是哪个季节,python中没有 && , || ,用 and 或 or 来表示 18 season = int(input(‘请输入一个月份:‘)) 19 if season >=3 and season <=5: 20 print(‘春季‘) 21 elif season >=6 and season <=8: 22 print(‘夏季‘) 23 elif season >=9 and season <=11: 24 print(‘秋季‘) 25 else: 26 print(‘冬季‘) 27 28 # 练习 29 小明身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数: 30 31 低于18.5:过轻 32 18.5-25:正常 33 25-28:过重 34 28-32:肥胖 35 高于32:严重肥胖 36 bmi = 80.5 / pow(1.75,2) 37 if bmi < 18.5: 38 str = ‘过轻‘ 39 elif bmi >= 18.5 and bmi < 25: 40 str = ‘正常‘ 41 elif bmi >=25 and bmi < 28: 42 str = ‘过重‘ 43 elif bmi >=28 and bmi < 32: 44 str = ‘肥胖‘ 45 else: 46 str = ‘严重肥胖‘ 47 print(str) 48 49 =================== if判断 end ========================""" 50 51 52 """=============== for 循环 begin ============================ 53 54 name = [‘tom‘,‘bob‘,‘tim‘] 55 for n in name: 56 print(n) 57 58 # 用for循环计算1-10的和 59 sum = 0 60 nums = [1,2,3,4,5,6,7,8,9,10] 61 for i in nums: 62 sum += i 63 print(sum) 64 65 # 用for循环计算1-100的值,range(x,y)函数用来生成一个大于x,小于y的列表,如果range(z)一个参数的情况下会生成一个大于等于 66 0,小于101的列表 67 sum = 0 68 for i in range(1,101): 69 sum += i 70 print(sum) 71 72 ================== for 循环 end============================""" 73 74 """=============== while 循环 begin ============================= 75 76 # 用while循环计算1-10的和 77 i = 1 78 sum = 0 79 while i<=10: 80 sum += i 81 i+=1 82 print(sum) 83 84 # 用while循环逆向输出10-1 85 num = 10 86 while num>=0: 87 print(num) 88 num = num-1 89 90 ================== while 循环 end ====结束时间:2018:11-23 0:06========================"""
原文地址:https://www.cnblogs.com/JiangBigShrimp/p/10005192.html
时间: 2024-10-10 13:38:56