算法1:

score = 85 # 假设成绩为85分

if score < 60: grade = "不合格" elif score < 70: grade = "合格" elif score < 80: grade = "中等" elif score < 90: grade = "良好" else: grade = "优秀"

print("成绩为{}分,等级为{}".format(score, grade))

算法2:

score = 85 # 假设成绩为85分

grade = "" # 先定义一个空字符串,用于存储等级

使用switch语句结构

注意:Python语言中没有原生的switch语句,可以用字典模拟实现

switcher = { 0: "不合格", 1: "合格", 2: "中等", 3: "良好", }

根据分数计算等级

index = int((score - 60) / 10) if index < 0: index = 0 if index > 3: index = 3 grade = switcher.get(index)

print("成绩为{}分,等级为{}".format(score, grade))


原文地址: https://www.cveoy.top/t/topic/boek 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录