第二周作业--第二章读书笔记
超市抹零结账行为
print("学号后四位:3103") price = float(input("请输入价格:")) rounded_price = round(price) print(f"原价:{price},抹零后的价格:{rounded_price}")
执行结果:
计算学生成绩的分差和平均分
print("学号后四位:3103") scores = input("请输入学生成绩(以逗号分隔):") # 输入学生成绩,以逗号分隔 score_list = [float(score) for score in scores.split(",")] # 将输入的字符串分割成一个分数列表 average_score = sum(score_list) / len(score_list) # 计算平均分 score_range = max(score_list) - min(score_list) # 计算最高分与最低分的分差 print("学生成绩列表:", score_list) print("平均分:", average_score) print("分差:", score_range)
执行结果:
使用运算符比较大小关系
print("学号后四位:3103") a = float(input("请输入第一个数:")) b = float(input("请输入第二个数:")) if a > b: print(f"{a} 大于 {b}") elif a < b: print(f"{a} 小于 {b}") else: print(f"{a} 等于 {b}")
执行结果:
手机店打折活动
print("学号后四位:3103") product_price = float(input("请输入产品价格: ")) # 获取产品价格 discount = float(input("请输入折扣(例如:输入0.5表示50%折扣):")) # 获取产品折扣 discounted_price = product_price * (1 - discount) # 计算折后价格 print(f"折扣后的价格为: {discounted_price:.2f}") # 显示折后价格
执行结果:
新建一个文件,并保存一段话:"2.6.2"
print("学号后四位:3103") text_to_save = "2.6.2" file_name = "my python file.txt" with open(file_name, "w") as file: file.write(text_to_save) print(f"文本已保存到 {file_name}")
执行结果: