# 成绩等级判定 # 方法一:正常分支解决即可 score = float(input('请输入成绩: ')) if score >= 90: grade = 'A' elif score >= 80: grade = 'B' elif score >= 70: grade = 'C' elif score >= 60: grade = 'D' else: grade = 'E' print('对应的等级是:', grade) # 方法二:利用字典进行解决,通过字典将相同等级的分数段作为字典的键,等级作为字典的键值 # 字典由键和键值构成,键可以类似关键字,键值对应键映射的值 score = float(input('请输入成绩: ')) grade_dict = { range(90, 101): 'A', range(80, 90): 'B', range(70, 80): 'C', range(60, 70): 'D', range(0, 60): 'E' } grade = None for key in grade_dict: if score in key: grade = grade_dict[key] break print('对应的等级是:', grade) # 方法三:较方法一嵌套的层数比较多,执行起来相对较慢 score = float(input('请输入成绩: ')) if score >= 90: grade = 'A' else: if score >= 80: grade = 'B' else: if score >= 70: grade = 'C' else: if score >= 60: grade = 'D' else: grade = 'E' print('对应的等级是:', grade)