Python Program to Calculate Student's Grade

Python | Calculate Student's Grade Program: In this tutorial, we will learn how to calculate a student's grade based on the given subject marks in Python using multiple approaches. By Pankaj Singh Last updated : June 05, 2023

Problem Statement

Given students details, marks in 5 subjects and we have to find student's grade.

In this program, we are taking student name, roll number and marks in 5 subjects and calculating student's grade based on the percentage and printing the all details.

Calculate Student's Grade

To calculate a student's grade using a Python program, you can simply input the student's details like roll number, name, and marks in specified subjects (here, in our examples - we are reading marks in 5 subjects). And, based on the given marks in the subjects, you can calculate the percentage and find and print the grade as per the below-given conditions:

Grades based on the percentage

  • If the percentage is >=85, then the grade will be "S".
  • If the percentage is >=75, then the grade will be "A".
  • If the percentage is >=65, then the grade will be "B".
  • If the percentage is >=55, then the grade will be "C".
  • If the percentage is >=50, then the grade will be "D".

Approaches to Calculate Student's Grade in Python

There are two approaches that we are using in this tutorial:

  1. Using Simple Approach
  2. Using Class and Object Approach

1. Calculate Student's Grade Using Simple Approach

In this approach, you can simply input the specified values and then calculate the total marks and percentage. Finally, write the conditions to check and print the grade.

Python Program to Calculate Student's Grade Using Simple Approach

# input values
roll = int(input("Enter Roll: "))
name = input("Enter Name: ")
print("Enter marks of 5 subjects: ")
marks = []
for i in range(5):
    marks.append(int(input("Subject " + str(i + 1) + ": ")))

# Find total
total = 0
for x in marks:
    total += x

# Find percentage
per = total / 5

# Find Grade
if per >= 85:
    grade = "S"
elif per >= 75:
    grade = "A"
elif per >= 65:
    grade = "B"
elif per >= 55:
    grade = "C"
elif per >= 50:
    grade = "D"
else:
    grade = "F"

# Print all details
print("Roll Number:", roll)
print("Name:", name)
print("Marks:")
for x in marks:
    print(x)
print("Grade:", grade)

Output:

Enter Roll: 101
Enter Name: Prem
Enter marks of 5 subjects: 
Subject 1: 78
Subject 2: 86
Subject 3: 78
Subject 4: 67
Subject 5: 98
Roll Number: 101
Name: Prem
Marks:
78
86
78
67
98
Grade: A

2. Calculate Student's Grade Using Class and Object Approach

In this approach, you can define a student class and its related methods to input the values, calculate the total marks & percentage, and check and print the grades and result.

Python Program to Calculate Student's Grade Using Class & Object Approach

# Python code to find student grade 

class Student:
    def __init__(self):
        self.__roll=0
        self.__name=""
        self.__marks=[]
        self.__total=0
        self.__per=0
        self.__grade=""
        self.__result=""

    def setStudent(self):
        self.__roll=int(input("Enter Roll: "))
        self.__name=input("Enter Name: ")
        print("Enter marks of 5 subjects: ")
        for i in range(5):
            self.__marks.append(int(input("Subject "+str(i+1)+": ")))
			
    def calculateTotal(self):
        for x in self.__marks:
            self.__total+=x
			
    def calculatePercentage(self):
        self.__per=self.__total/5
		
    def calculateGrade(self):
        if self.__per>=85:
            self.__grade="S"
        elif self.__per>=75:
            self.__grade="A"
        elif self.__per>=65:
            self.__grade="B"
        elif self.__per>=55:
            self.__grade="C"
        elif self.__per>=50:
            self.__grade="D"
        else:
            self.__grade="F"
			
    def calculateResult(self):
        count=0
        for x in self.__marks:
            if x>=50:
                count+=1
        if count==5:
            self.__result="PASS"
        elif count>=3:
            self.__result="COMP."
        else:
            self.__result="FAIL"
			
    def showStudent(self):
        self.calculateTotal()
        self.calculatePercentage()
        self.calculateGrade()
        self.calculateResult()
        print(self.__roll,"\t\t",self.__name,"\t\t",self.__total,"\t\t",self.__per,"\t\t",self.__grade,"\t\t",self.__result)


def main():
    #Student object
    s=Student()
    s.setStudent()
    s.showStudent()

if __name__=="__main__":
    main()

Output

Enter Roll: 101
Enter Name: Prem Sharma
Enter marks of 5 subjects:
Subject 1: 22
Subject 2: 33
Subject 3: 55
Subject 4: 66
Subject 5: 77
101              Prem Sharma             253             50.6            D
COMP.

Python class & object programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.