1.在终端中输入整数,打印正数,负数,零
number = int(input("请输入整数")) if number > 0: print("正数") elif number < 0: print("负数") else: print("零")
2.在终端中输入课程阶段数,显示课程名称
""" 在终端中输入课程阶段数,显示课程名称 输入: 输出: 1 Python语言核心编程 2 Python高级软件技术 3 Web 全栈 4 人工智能 """ stage_number = int(input("请输入阶段:")) if stage_number == 1: print("Python语言核心编程") elif stage_number == 2: print("Python高级软件技术") elif stage_number == 3: print("Web 全栈") elif stage_number == 4: print("人工智能") else: print("暂无对应课程")
3.在终端中录入4个同学身高,打印最高的值
student_height01 = int(input("请输入学生1身高:")) student_height02 = int(input("请输入学生2身高:")) student_height03 = int(input("请输入学生3身高:")) student_height04 = int(input("请输入学生4身高:")) # 求最大值,最小值都可定义一个中间变量 max_height = student_height01 if max_height < student_height02: max_height = student_height02 if max_height < student_height03: max_height = student_height03 if max_height < student_height04: max_height = student_height04 print("学生的最高身高是:" + str(max_height))
4.根据心理年龄与实际年龄,打印智商等级
""" 智商IQ = 心理年龄MA 除以 实际年龄CA 乘以 100 天才:140以上(包含) 超常:120-139之间(包含) 聪慧:110-119之间(包含) 正常:90-109之间(包含) 迟钝:80-89之间(包含) 低能:80以下 """ # 错误示范 # 下列写法漏掉了139~140,119~120,109~110,89~90 MA = int(input("请输入心理年龄:")) CA = int(input("请输入实际年龄:")) IQ = MA / CA * 100 if IQ >= 140: print("天才") elif 120 <= IQ < 139: print("超常") elif 110 <= IQ < 119: print("聪慧") elif 90 <= IQ < 109: print("正常") elif 80 <= IQ < 89: print("迟钝") else: print("低能") # 错误示范,过度判断,能正确运行,可读性不好 # 核心if elif 是互斥的 MA = int(input("请输入心理年龄:")) CA = int(input("请输入实际年龄:")) IQ = MA / CA * 100 if IQ >= 140: print("天才") elif 120 <= IQ < 140: print("超常") elif 110 <= IQ < 120: print("聪慧") elif 90 <= IQ < 110: print("正常") elif 80 <= IQ < 90: print("迟钝") else: print("低能") # 推荐写法 # 核心if elif 是互斥的 # 参考坐标系y轴 MA = int(input("请输入心理年龄:")) CA = int(input("请输入实际年龄:")) IQ = MA / CA * 100 if IQ >= 140: print("天才") elif 120 <= IQ: print("超常") elif 110 <= IQ: print("聪慧") elif 90 <= IQ: print("正常") elif 80 <= IQ: print("迟钝") else: print("低能")
5.在终端中输入月份,打印相应的天数.
""" 1 3 5 7 8 10 12 有 31天 2 有 29天 4 6 9 11 有 30天 超过月份提示月份有误 """ month = int(input("请输入月份:")) if 1 <= month < 13: if month == 2: print(str(month) + "月有29天") elif month == 4 or month == 6 or month == 9 or month == 11: print(str(month) + "月有30天") else: print(str(month) + "月有31天") else: print("输入的月份有误")
6.让代码重复执行,按y继续(反之退出)
while True: number = int(input("请输入整数:")) if number > 0: print("正数") elif number < 0: print("负数") else: print("零") if input("输入y继续") != "y": print("已退出") break
7.while循环计数
""" # 在终端中显示0 1 2 3 # 在终端中显示2 3 4 5 6 # 在终端中显示1 3 5 7 # 在终端中显示8 7 6 5 4 # 在终端中显示-1 -2 -3 -4 -5 """ count = 0 while count < 4: print(count) count += 1 count = 2 while count < 7: print(count) count += 1 count = 1 while count < 8: print(count) count += 2 count = 8 while count > 3: print(count) count -= 1 count = -1 while count > -6: print(count) count += -1
8.在终端循环录入5个成绩,打印平均成绩
count = 0 total_score = 0 while count < 5: total_score += int(input("请输入学生成绩:")) count += 1 print("学生的平均分是:" + str(total_score / count))
9.求纸对折几次能超过珠峰
""" 一张纸的厚度是0.01毫米 请计算,对折多少次超过珠穆朗玛峰(8844.43米) """ paper_height = 0.01 mount_everest_height = 8844.43 * 1000 # 米转换为毫米 count = 0 while paper_height < mount_everest_height: paper_height *= 2 count += 1 print("纸对折" + str(count) + "次超过珠穆朗玛峰")
10.猜数字
""" 猜数字 程序产生1个,1到100之间的随机数。 让玩家重复猜测,直到猜对为止。 每次提示:大了、小了、恭喜猜对了,总共猜了多少次。 """ import random random_number = random.randint(1, 100) count = 0 while True: number = int(input("请输入猜测的点数")) count += 1 if number > random_number: print("猜大了") elif number < random_number: print("猜小了") else: print("猜对了") break print("猜测了" + str(count) + "次")
11.终端输入任意整数,求各个位数累加和
number_str = input("请输入整数:") total_number = 0 for item in number_str: total_number += int(item) print(total_number)
12.for 与 range
""" 在终端中累加 0 1 2 3 在终端中累加 2 3 4 5 6 在终端中累加 1 3 5 7 在终端中累加 8 7 6 5 4 在终端中累加 -1 -2 -3 -4 -5 """ total_number = 0 for number in range(4): total_number += number print(total_number) total_number = 0 for number in range(2, 7): total_number += number print(total_number) total_number = 0 for number in range(1, 8, 2): total_number += number print(total_number) # 在终端中累加 8 7 6 5 4 # 写法1 total_number = 0 for number in range(-8, -3): total_number += -number print(total_number) # 写法2 推荐 total_number = 0 for number in range(8, 3, -1): total_number += number print(number) print(total_number) # 在终端中累加 - 1 - 2 - 3 - 4 - 5 # 写法1 total_number = 0 for number in range(1, 6): total_number += -number print(total_number) # 写法2 total_number = 0 for number in range(-1, -6, -1): total_number += number print(total_number)
13.累加10~60直接,个位不是3\5\8的整数和
""" 练习:累加10 -- 60之间,个位不是3/5/8的整数和。 -- 思想1:不是3/5/8则累加 -- 思想2:是3/5/8跳过否则累加 """ # 写法1 total_number = 0 for number in range(10,61): if number != 3 and number != 5 and number != 8: total_number += number print(total_number) # 写法2 total_number = 0 for number in range(10,61): if number == 3 or number == 5 or number == 8: continue total_number += number print(total_number)
14.判断程序运行结果
""" 2. 写出下列程序运行结果: (1) input_number = 8 random_number = 8 if input_number == random_number: print("猜对了") # 运行这一行,两变量的值都是8 else: print("猜错了") (2) num = 12 if num > 3: print("⼤于3") # 因为num大于3,判断条件顺序写反了 elif num > 5: print("⼤于5") elif num > 10: print("⼤于10") elif num > 15: print("⼤于15") """
15.电梯超载提示
""" (1) 电梯设置规定: 如果承载⼈不超过10⼈,且总重量不超过1000千克,可以正常使⽤,否则提示超载。 步骤: 终端中获取人数/总重量 显示电梯正常运行 电梯超载 """ people_number = 0 total_people_weight = 0 while True: people_weight = int(input("请输入乘坐电梯人员的体重:")) total_people_weight += people_weight people_number += 1 print(people_number, total_people_weight) if people_number < 11 and 1000 <= total_people_weight: print("电梯正常运行") else: print("电梯超载") break
16.根据年龄,判断人生阶段
""" 根据年龄,输出对应的人生阶段。 年龄 ⼈⽣阶段 0-6 岁 童年 7-17 岁 少年 18-40 岁 ⻘年 41-65 岁 中年 65 岁之后 ⽼年 步骤: 终端中获取年龄 显示人生阶段 """ age = int(input("请输入年龄:")) if age >= 65: print("老年") elif age >= 41: print("中年") elif age >= 18: print("青年") elif age >= 7: print("少年") else: print("童年")
17.顾客打折
""" 如果是vip客户,消费小于等于500,享受85折 消费大于500,享受8折 如果不是vip客户,消费大于等于800,享受9折 消费小于800,原价 在终端中输入账户类型,消费金额,计算折扣. """ shopper_type = input("请输入顾客类型:") shopper_consumption = int(input("请输入顾客消费金额:")) if shopper_type == "vip": if shopper_consumption > 500: print(shopper_type + "顾客享受8折打折后应付" + str(shopper_consumption * 0.8)) else: print(shopper_type + "顾客享受8.5折打折后应付" + str(shopper_consumption * 0.85)) else: if shopper_consumption >= 800: print("不是vip顾客享受9折打折后应付" + str(shopper_consumption * 0.9)) else: print("不是vip顾客不打折应付" + str(shopper_consumption))
18.在终端中录入4个同学体重,打印最轻的值
student_weight01 = int(input("请输入学生体重:")) student_weight02 = int(input("请输入学生体重:")) student_weight03 = int(input("请输入学生体重:")) student_weight04 = int(input("请输入学生体重:")) min_weight = student_weight01 if min_weight > student_weight02: min_weight = student_weight02 if min_weight > student_weight03: min_weight = student_weight03 if min_weight > student_weight04: min_weight = student_weight04 print(min_weight)
19.赌大小
import random total_money = 10000 while total_money > 0: betting = int(input("少侠请下注:")) gambler_point = random.randint(1, 6) # 赌徒 makers_point = random.randint(1, 6) # 庄家 if betting > total_money: print("超出了你的身家,请重新投注。") continue print("你摇出了" + str(gambler_point) + "点, 庄家摇出了" + str(makers_point) + "点") if gambler_point > makers_point: total_money += betting print("恭喜啦,你赢了,继续赌下去早晚会输光的,身家还剩" + str(total_money)) elif gambler_point < makers_point: total_money -= betting print("少侠, 你输了,身家还剩" + str(total_money)) else: print("打平了,少侠,在来一局?") print("哈哈哈,少侠你已经破产,无资格进行游戏")
标签:语句,count,python,number,while,student,print,input,total From: https://www.cnblogs.com/xmgcool/p/16946033.html