Python program to calculate total marks percentage and grade of a student
In this post, you will learn how to calculate total marks percentage and grade of a student using the Python programming language. For this, we are taking the marks of five subjects and calculating a student's grade based on the percentage of those five subjects.
The given Python program allows a user to enter marks for five subjects. For this, we have defined a class StudentResult and taken five parameters from calling the function. Next, we have defined different functions to calculate the total, percentage of those five subjects, grade, and display the result.
# 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
Related Articles
How to find the stop words in nltk Python
Python OpenCV Histogram of Color Image
Python OpenCV Image Filtering
Reverse pyramid pattern in Python
Fizzbuzz program in Python
Python split string by comma
Python alive progress bar
Python progress bar tqdm
Python language translator code
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Reverse a number in Javascript
Bouncing ball game code in Python
HTML open link in new tab
Python print without newline
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword