Q:

Write a python program to calculate total marks percentage and grade of a student

0

Write a python program to calculate total marks percentage and grade of a student

In this exercise, you will learn how to find the grade of a student. For this, we are taking the marks of five subjects and calculating a student's grade based on the percentage of those five subjects.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The following Python program allows a user to enter five different values for five subjects. For this, we have defined a class and taken five parameters from calling the function. Next, we have defined different functions to calculate the total and percentage of those five subjects.

# Python code to find student grade 

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

    def studentData(self):
        self.__roll=int(input("Enter student roll: "))
        self.__name=input("Enter student name: ")
        print("Enter marks of five subjects: ")
        for i in range(5):
            self.__marks.append(int(input("Subject "+str(i+1)+": ")))
			
    def calTotalMarks(self):
        for x in self.__marks:
            self.__total+=x
			
    def calPercentage(self):
        self.__per=self.__total/5
		
    def calGrade(self):
        if self.__per>=85:
            self.__grade="A+"
        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 getResult(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.studentData()
        self.calTotalMarks()
        self.calPercentage()
        self.calGrade()
        self.getResult()
        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():
    # object is created for StudentResult
    s=StudentResult()
    s.showStudent()

if __name__=="__main__":
    main()

Output of the above code - 

Enter student roll: 101
Enter student name: Priska Kashyap
Enter marks of five subjects:
Subject 1: 78
Subject 2: 76
Subject 3: 64
Subject 4: 83
Subject 5: 66
101              Priska Kashyap                  367             73.4            B               PASS

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a python program to find the area of a recta... >>
<< Write a program to calculate simple interest in Py...