""" if语句 """ score = 59 if score < 60: print('成绩不及格') else: print('成绩及格') # 年龄判断 age = 19 if age >= 18: print('adult') else: print('teenager') """ 在100分的考试当中,分数达到或超过90分是顶尖的,达到或超过80分是优秀的, 如果想对达到或者超过60分的学生进行不同的分类,使用if-else进行判断 """ score = 59 if score >= 90: print('顶尖') elif score >= 80: print('优秀') elif score >= 60: print('及格') else: print('不及格') # 代码从上往下执行,因先满足if条件,故先执行if条件语句,而不是elif条件 score = 95 if score >= 80: print('优秀') elif score >= 90: print('顶尖') elif score >= 60: print('及格') else: print('不及格') """ 如果年龄达到18岁,则是成年人,如果年龄6岁到18岁,则是青少年,如果年龄3岁到6岁,则是小孩子,如果年龄在3岁以下,则是婴儿, 请使用if-elif-else语句实现逻辑, 如果成年,输出'adult',如果是青少年,输出'teenager',如果是小孩子,输出kid,如果是婴儿,输出baby。 """ age = 0 if age >= 18: print('adult') elif 6 <= age < 18: print('teenager') elif 3 <= age and age < 6: print('kid') elif 0 < age < 3: print('baby') else: print('error code') """ for语句 班里考试后,老师要统计几位同学的平均成绩,已知5位同学的成绩用list表示如下: L = [75, 92, 59, 68, 99],请利用for循环计算出平均成绩。 """ L = [75, 92, 59, 68, 99] sum1 = 0 for i in L: sum1 += i avg = sum1 / len(L) print(avg) # 78.6 """ while语句 计算1~100的和 """ i = 1 sum2 = 0 while i <= 100: sum2 += i i += 1 print(sum2) # 5050 """ while语句 计算1~10的乘积 """ i = 1 sum3 = 1 while i <= 10: sum3 *= i i += 1 print(sum3) # 3628800 """ while之跳出循环 """ num = 1 sum = 0 while True: if num > 100: break sum += num num += 1 print(sum) s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' num = 0 for i in s: if num > 19: break print(i) num += 1 """ 请综合使用while True和break,计算0~1000以内,所有偶数的和 """ a = 0 sum4 = 0 while a <= 1000: if a % 2 == 0: sum4 += a a += 1 else: a += 1 print(sum4) # 250500 a = 0 sum5 = 0 while True: if a > 1000: break if a % 2 == 0: sum5 += a a += 1 else: a += 1 print(sum5) # 250500 """ 使用两层嵌套循环输出字符串'ABC'中每个字符和字符串'123'每个字符的排列 同理,再加一组数据,可以组合更多的排列 """ s1 = 'ABC' s2 = '123' for x in s1: # print(type(x)) # 所以可以进行字符串的拼接 for y in s2: print(x + y) s1 = 'ABC' s2 = '123' s3 = 'xyz' for x in s1: for y in s2: for z in s3: print(x + y + z)
标签:控制,elif,num,流程,day2,else,break,score,print From: https://www.cnblogs.com/purewhite/p/17179532.html