Files
2026-07-22 16:42:56 +08:00

67 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 第 1-5 课示例:条件判断
#
# 本程序根据考核分数和练习完成状态给出不同结果。
# 建议使用讲义中的多组数据反复运行,观察每次进入了哪个分支。
# 一、接收考核分数
# input() 返回字符串,因此先保留原始文本,再转换为浮点数。
score_text = input("请输入考核分数0 到 100")
score = float(score_text)
# 二、接收练习状态
# 本示例约定用户输入 yes 表示已完成,输入其他内容表示未完成。
practice_answer = input("是否完成练习yes/no")
practice_finished = practice_answer == "yes"
# 三、先判断分数范围,再判断成绩等级
# 条件按从特殊到一般、从高分到低分的顺序排列。
if score < 0 or score > 100:
score_level = "输入无效"
elif score >= 90:
score_level = "优秀"
elif score >= 80:
score_level = "良好"
elif score >= 60:
score_level = "合格"
else:
score_level = "未通过"
print(f"成绩等级:{score_level}")
# 四、组合多个条件决定能否继续
# 只有分数处于有效范围、达到 60 分并且完成练习时,才允许继续。
score_is_valid = 0 <= score <= 100
score_is_passed = score >= 60
if score_is_valid and score_is_passed and practice_finished:
print("课程和练习均已通过,可以进入下一课。")
else:
print("当前要求尚未全部完成,暂时不能进入下一课。")
# 五、使用嵌套判断给出更具体的提示
# 先确认分数有效,之后才进一步分析未满足的项目。
if score_is_valid:
if not score_is_passed:
print("改进建议:重新复习知识点并再次完成测验。")
elif not practice_finished:
print("改进建议:成绩已经通过,请继续完成课堂练习。")
else:
print("学习建议:保持当前节奏,继续学习下一课。")
else:
print("输入提示:分数必须位于 0 到 100 之间。")
# 六、演示空字符串的真值
# 如果用户没有输入姓名而直接按回车user_name 是空字符串,条件结果为假。
user_name = input("请输入你的名字或称呼:")
if user_name:
print(f"本次评估学习者:{user_name}")
else:
print("本次评估未填写学习者名称。")