用 Python 制作成绩转换器

Fariba Laiq 2022年5月26日
用 Python 制作成绩转换器

成绩计算器/转换器是根据学生获得的每个分数范围分配成绩的程序。这种程序经常被教师和教育机构使用,通过输入每个科目的个人分数来自动计算成绩。

用 Python 制作成绩转换器

可以有多种方案和标准来计算学生的成绩。

在这里,我们将制作两种类型的成绩转换器。一个是简单的,另一个是加权的。

简单成绩转换器

在这里,我们将制作一个成绩转换器,它将计算成绩而不给分数赋予任何权重。以下是步骤:

  • 用户将输入学生在每个科目中获得的个人分数。
  • 然后,我们将通过将每个科目的分数之和除以科目数来计算平均分数。
  • 最后,我们将使用 if-else 条件为每个平均分数范围分配一个成绩。

示例代码:

# Python 3.x
total_marks = 0
print("Enter Marks Obtained in 4 Subjects: ")
for i in range(1, 5):
    marks = int(input("Enter the marks for subject:" + str(i) + " "))
    total_marks = total_marks + marks
avg_marks = total_marks / 4
if avg_marks >= 91 and avg_marksg <= 100:
    print("Grade: A+")
elif avg_marks >= 81 and avg_marks < 91:
    print("Grade: A")
elif avg_marks >= 71 and avg_marks < 81:
    print("Grade: B+")
elif avg_marks >= 61 and avg_marks < 71:
    print("Grade: B")
elif avg_marks >= 51 and avg_marks < 61:
    print("Grade: C+")
elif avg_marks >= 41 and avg_marks < 51:
    print("Grade: C")
else:
    print("Grade: F")

输出:

#Python 3.x
Enter Marks Obtained in 4 Subjects:
Enter the marks for subject:1 90
Enter the marks for subject:2 80
Enter the marks for subject:3 60
Enter the marks for subject:4 70
Grade: B+

加权成绩转换器

在这种情况下,成绩转换器将通过添加一个额外的步骤来计算成绩,该步骤赋予考试、实验室任务和作业中获得的分数权重。在这里,我们将输入考试、实验任务和作业的分数,并一一计算它们的平均值。

最后,我们将通过赋予每个分数权重来计算总平均分数。我们将权重定义如下:

  1. 10%的权重属于作业的平均分。
  2. 20%的权重属于实验室任务的平均分。
  3. 70%的权重属于考试的平均分。

计算总平均分后,我们将根据每个分数范围分配成绩。

示例代码:

# Python 3.x
total_marks = 0
exam1 = int(input("Enter Marks Obtained in Exam 1: "))
exam2 = int(input("Enter Marks Obtained in Exam 2: "))
avg_exam = (exam1 + exam2) / 2
totallab = 0
totalassignment = 0
for i in range(1, 3):
    marks = int(input("Enter Marks Obtained in Lab Task: " + str(i) + " "))
    totallab = totallab + marks
avglab = totallab / 2
for i in range(1, 5):
    marks = int(input("Enter the marks for Assignment:" + str(i) + " "))
    totalassignment = totalassignment + marks
avgassignment = totalassignment / 4
avg_marks = 0.1 * avgassignment + 0.7 * avg_exam + 0.2 * avglab
if avg_marks >= 91 and avg_marksg <= 100:
    print("Grade: A+")
elif avg_marks >= 81 and avg_marks < 91:
    print("Grade: A")
elif avg_marks >= 71 and avg_marks < 81:
    print("Grade: B+")
elif avg_marks >= 61 and avg_marks < 71:
    print("Grade: B")
elif avg_marks >= 51 and avg_marks < 61:
    print("Grade: C+")
elif avg_marks >= 41 and avg_marks < 51:
    print("Grade: C")
else:
    print("Grade: F")

输出:

#Python 3.x
Enter Marks Obtained in Exam 1: 40
Enter Marks Obtained in Exam 2: 60
Enter Marks Obtained in Lab Task: 1 20
Enter Marks Obtained in Lab Task: 2 40
Enter the marks for Assignment:1 60
Enter the marks for Assignment:2 10
Enter the marks for Assignment:3 30
Enter the marks for Assignment:4 40
Grade: C
作者: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.

LinkedIn

相关文章 - Python Math