如何用Python创建评分系统?

Lep*_*mos -9 python

我有一项学校作业,其中一项任务是显示学生将获得的成绩。等级为:

  • 答:90%+
  • 乙:80% - 89%
  • C: 70% - 79%
  • 深度:60% - 69%
  • 乙:50% - 59%

以下是该文件的一些内容,它是一个以逗号分隔的 CSV 文件:

StudentName Score
Harrison    64
Jake    68
Jake    61
Hayley  86
Run Code Online (Sandbox Code Playgroud)

我想知道/获得一些指导,以便我更好地了解如何创建成绩计算器。我花了很长时间试图解决这个问题,但没有希望。我的代码:

def determine_grade(scores):
    if scores >= 90 and <= 100:
        return 'A'
    elif scores >= 80 and <= 89:
        return 'B'
    elif scores >= 70 and <= 79:
        return 'C'
    elif scores >= 60 and <= 69:
        return 'D'
    elif scores >= 50 and <= 59:
        return 'E'
    else:
        return 'F'
Run Code Online (Sandbox Code Playgroud)

小智 6

您可以使用bisectPython 的标准库来实现此目的。

import bisect 

def determine_grade(scores, breakpoints=[50, 60, 70, 80, 90], grades='FEDCBA'):
    i = bisect.bisect(breakpoints, scores)
    return grades[i]
Run Code Online (Sandbox Code Playgroud)

  • 尝试通过一些解释来改进你的答案。特别是您正在使用一些人可能不熟悉的外部库。在这种情况下,链接也非常有用,例如供进一步参考 (5认同)
  • 这基本上是“bisect”中示例之一的逐字记录(内置库,而不是您所说的外部库。https://docs.python.org/3/library/bisect.html#other-examples -请注意,当我们有重要的数据集时,这个解决方案要好得多,例如,如果我们有 5000 个不同的等级,那么 if-else 将根本不起作用。 (3认同)