Python Program to add marks and calculate the grade of a student


December 15, 2022, Learn eTutorial
2714

This is a simple student level python program to determine the grade of a student based on the marks obtained in five subjects.

To understand this example, you should have knowledge of the following Python programming topics:

How to find the grade of a student in python?

To find the grade of a student we need his marks obtained in each subject and the maximum mark of a subject to which exam conducted. We read these values from the user and calculate the percentage of marks he obtained. According to the percentage got displays the appropriate grade to the user.

It's a beginner-level program as we need to input the marks of each subject of the student and calculate the percentage with simple mathematical operations only. Adding the marks of subjects and dividing it with total subjects will give average marks by using python methods and basics. Now from the average of the marks, we calculate the percentage of marks by dividing it with total marks for the exam, followed by multiplication with 100. Thus calculate the grade using if condition and the elif in python and print grades depend on the percentage of marks.

For example, let us take a case of students who got marks as 8, 7, 9, 6, 8 out of 10. The average of the marks is 7.6, the percentage is 76 and then we calculate the grade. A or B or C or D or E depends on percentages 90, 80, 70, 60, and 50. So here, the grade is C.

Note: According to the grade bars, Grading can be done with the appropriate change in mark percentage given in the if-elif conditions

ALGORITHM

STEP 1: Accept the maximum mark of a subject, marks of each subject separately from the user using the input method. Then convert that string to integer using int().

STEP 2: Calculate the average by dividing the sum of the marks by the total number of subjects, and percentage by dividing the average by the maximum mark of subjects, multiplying that with 100.

STEP 3: Use the if condition to check the grade using the value from the percentage. If the percentage is above 90 then print A.

STEP 4: Use elif statements to print the grades from B, C, D, depending on the percentage marks like 80, 70, 60.

STEP 5: Use the else condition to print grade E if the percentage value is less than 60.

Python Source Code

                                          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")

                                      

OUTPUT

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