用于添加分数并计算学生成绩的Python程序


2022年12月15日, Learn eTutorial
3944

这是一个简单的学生级Python程序,用于根据学生在五个科目中获得的分数来确定学生的成绩。

要理解这个例子,您应该了解以下 Python 编程主题

如何在Python中查找学生的成绩?

要查找学生的成绩,我们需要他在每个科目中获得的分数以及考试中每个科目的最高分数。我们从用户那里读取这些值并计算他获得的分数百分比。根据获得的百分比向用户显示相应的成绩。

这是一个初级程序,因为我们只需要输入学生每个科目的分数,并仅通过简单的数学运算计算百分比。通过使用Python方法和基础知识,将科目的分数相加并除以总科目数,即可得到平均分数。现在,根据平均分数,通过将其除以考试总分数,然后乘以100,计算分数的百分比。因此,使用if条件和Python中的elif计算成绩,并根据分数的百分比打印成绩。

例如,我们来看一个学生在10分中得了8、7、9、6、8分的情况。平均分是7.6,百分比是76,然后我们计算成绩。A、B、C、D或E取决于90、80、70、60和50的百分比。所以这里,成绩是C。

注意:根据成绩等级,可以在if-elif条件中适当更改分数百分比来完成评分

算法

步骤1:使用输入方法从用户那里分别接受科目的最高分数和每个科目的分数。然后使用int()将其字符串转换为整数。

步骤2:通过将分数总和除以科目总数来计算平均值,并通过将平均值除以科目的最高分数,然后乘以100来计算百分比

步骤3:使用if条件根据百分比值检查成绩。如果百分比高于90,则打印A

步骤4:使用elif语句根据80、70、60等百分比分数打印B、C、D等级。

步骤5:如果百分比值小于60,则使用else条件打印E等级。

Python 源代码

                                          total=int(input("Enter the maximum mark of a subject: "))

sub1=int(input("Enter marks of the first subject: "))

sub2=int(input("Enter marks of the second subject: "))

sub3=int(input("Enter marks of the third subject: "))

sub4=int(input("Enter marks of the fourth subject: "))

sub5=int(input("Enter marks of the fifth subject: "))

average=(sub1+sub2+sub3+sub4+sub5)/5 
print("Average is ",average)
percentage=(average/total)*100
print("According to the percentage, ")
if percentage >= 90:
    print("Grade: A")
elif percentage >= 80 and percentage < 90:
    print("Grade: B")
elif percentage >= 70 and percentage < 80:
    print("Grade: C")
elif percentage >= 60 and percentage < 70:
    print("Grade: D")
else:
    print("Grade: E")

                                      

输出

Enter the maximum mark of a subject: 10
Enter marks of the first subject: 8
Enter marks of the second subject: 7
Enter marks of the third subject: 9
Enter marks of the fourth subject: 6
Enter marks of the fifth subject: 8
Average is  7.6
According to the percentage, 
Grade: C