You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

成绩计算器(if else语句):仅输出4条结果而非15条的问题排查

成绩计算器输出结果缺失问题解决

我编写了一个基于给定规则返回学生成绩评级的简单成绩计算器,但程序仅输出4条结果(预期应为15条),相关代码及输出如下:

students_score = [57, 89, 23, 26, 68, 95, 100, 58, 51, 90, 18, 42, 49, 51, 67]
students_grades = {}

print("Students score in percents: " + '\n', students_score, '\n')


def grade_counter():
    for i in students_score:
        if i in range(51, 101):
            good_grade = 'You passed the exam :D'
            if i in range(90, 101):
                students_grades.update({'5': f'{i} % , {good_grade}'})
            elif i in range(70, 90):
                students_grades.update({'4': f'{i} %, {good_grade}'})
            elif i in range(51, 70):
                students_grades.update({'3': f'{i} %, {good_grade}'})
        else:
            bad_grade = 'You fail the exam :('
            if i in range(0, 51):
                students_grades.update({'2': f'{i} %, {bad_grade}'})

    print(students_grades)


grade_counter()

输出结果:

Students score in percents: 
 [57, 89, 23, 26, 68, 95, 100, 58, 51, 90, 18, 42, 49, 51, 67] 

{'3': '67 %, You passed the exam :D', '4': '89 %, You passed the exam :D', '2': '49 %, You fail the exam :(', '5': '90 % , You passed the exam :D'}

问题原因:

你使用了字典students_grades存储结果,字典的键是固定的'2'、'3'、'4'、'5'。每次循环遇到对应评级的分数时,都会用新值覆盖同一个键的旧值,最终每个评级只保留最后一次处理的分数,因此仅输出4条结果。

解决方案:

根据需求,提供两种常见修改方式:

方式1:按评级分组存储所有对应分数

初始化字典时,将每个评级对应的设为列表,循环时向对应列表添加结果:

students_score = [57, 89, 23, 26, 68, 95, 100, 58, 51, 90, 18, 42, 49, 51, 67]
# 初始化每个评级对应的空列表
students_grades = {'2': [], '3': [], '4': [], '5': []}

print("Students score in percents: " + '\n', students_score, '\n')


def grade_counter():
    for i in students_score:
        if i in range(51, 101):
            good_grade = 'You passed the exam :D'
            if i in range(90, 101):
                students_grades['5'].append(f'{i} % , {good_grade}')
            elif i in range(70, 90):
                students_grades['4'].append(f'{i} %, {good_grade}')
            elif i in range(51, 70):
                students_grades['3'].append(f'{i} %, {good_grade}')
        else:
            bad_grade = 'You fail the exam :('
            if i in range(0, 51):
                students_grades['2'].append(f'{i} %, {bad_grade}')

    print(students_grades)


grade_counter()

输出示例:

Students score in percents: 
 [57, 89, 23, 26, 68, 95, 100, 58, 51, 90, 18, 42, 49, 51, 67] 

{'2': ['23 %, You fail the exam :(', '26 %, You fail the exam :(', '18 %, You fail the exam :(', '42 %, You fail the exam :(', '49 %, You fail the exam :('], '3': ['57 %, You passed the exam :D', '68 %, You passed the exam :D', '58 %, You passed the exam :D', '51 %, You passed the exam :D', '51 %, You passed the exam :D', '67 %, You passed the exam :D'], '4': ['89 %, You passed the exam :D'], '5': ['95 % , You passed the exam :D', '100 % , You passed the exam :D', '90 % , You passed the exam :D']}

方式2:保留每个学生的单独记录

用列表存储每个学生的评级结果,或者用学生索引作为字典键:

students_score = [57, 89, 23, 26, 68, 95, 100, 58, 51, 90, 18, 42, 49, 51, 67]
students_grades = []

print("Students score in percents: " + '\n', students_score, '\n')


def grade_counter():
    for i in students_score:
        if i in range(51, 101):
            good_grade = 'You passed the exam :D'
            if i in range(90, 101):
                students_grades.append(f'{i} % , 评级5, {good_grade}')
            elif i in range(70, 90):
                students_grades.append(f'{i} %, 评级4, {good_grade}')
            elif i in range(51, 70):
                students_grades.append(f'{i} %, 评级3, {good_grade}')
        else:
            bad_grade = 'You fail the exam :('
            if i in range(0, 51):
                students_grades.append(f'{i} %, 评级2, {bad_grade}')

    # 逐行输出每个学生的结果
    for idx, result in enumerate(students_grades, 1):
        print(f"学生{idx}: {result}")


grade_counter()

输出示例:

Students score in percents: 
 [57, 89, 23, 26, 68, 95, 100, 58, 51, 90, 18, 42, 49, 51, 67] 

学生1: 57 %, 评级3, You passed the exam :D
学生2: 89 %, 评级4, You passed the exam :D
学生3: 23 %, 评级2, You fail the exam :(
学生4: 26 %, 评级2, You fail the exam :(
学生5: 68 %, 评级3, You passed the exam :D
学生6: 95 % , 评级5, You passed the exam :D
学生7: 100 % , 评级5, You passed the exam :D
学生8: 58 %, 评级3, You passed the exam :D
学生9: 51 %, 评级3, You passed the exam :D
学生10: 90 % , 评级5, You passed the exam :D
学生11: 18 %, 评级2, You fail the exam :(
学生12: 42 %, 评级2, You fail the exam :(
学生13: 49 %, 评级2, You fail the exam :(
学生14: 51 %, 评级3, You passed the exam :D
学生15: 67 %, 评级3, You passed the exam :D

内容的提问来源于stack exchange,提问作者Pythonman21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 15:45:08