Program to create grade calculator in Python


In Academics it is a common requirement to find the grades of students after an assessment. In this article we will create a Python program which will assign the grades based on the grading criteria. Will call it A grade calculator.

Grading criteria

Below is the grading criteria we have chosen for the program.

score >= 90 : "O"
score >= 80 : "A+"
score >= 70 : "A"
score >= 60 : "B+"
score >= 50 : "B"
score >= 40 : "C"

Program Approach

  • Initialize variables and array to hold the student details including the marks obtained by individual subjects.

  • Define a function to accept input values on the screen and store them in the above variables.

  • Design a for loop to add the marks obtained in individual subjects.

  • Using if and elif condition design the calculator which will define the range of the marks obtained by the student and categorize the result into specific grade.

  • Finally define a function which will run the above functions in a specific sequence.

  • Run the program and input the values.

Grading Program

Below is the grading program as per the above approach. When we run the program it asks for various inputs. On feeding the require input, we get the final result.

Example

class grade_calculator:
   def __init__(self):
      self.__roll_number = 0
      self._Name = ""
      self.__marks_obtained = []
      self.__total_marks = 0
      self.__percentage = 0
      self.__grade = ""
      self.__result = ""
   def setgrade_calculator(self):
      self.__roll_number = int(input("Enter Roll Number: "))
      self.__Name = input("Enter Name: ")
      print("Enter 5 subjects marks: ")
      for n in range(5):
         self.__marks_obtained.append(int(input("Subject " + str(n + 1) + ": ")))
   def Total(self):
      for i in self.__marks_obtained:
         self.__total_marks += i
   def Percentage(self):
      self.__percentage = self.__total_marks / 5
   def calculateGrade(self):
      if self.__percentage >= 90:
         self.__grade = "0"
      elif self.__percentage >= 80:
         self.__grade = "A+"
      elif self.__percentage >= 70:
         self.__grade = "A"
      elif self.__percentage >= 60:
         self.__grade = "B+"
      elif self.__percentage >= 50:
         self.__grade = "B"
      elif self.__percentage >= 40:
         self.__grade = "C"
      else:
         self.__grade = "F"
   def Result(self):
      count = 0
      for x in self.__marks_obtained:
         if x >= 40:
            count += 1
      if count == 5:
         self.__result = "PASS"
      elif count >= 3:
         self.__result = "COMP."
      else:
         self.__result = "FAIL"
   def showgrade_calculator(self):
      self.Total()
      self.Percentage()
      self.calculateGrade()
      self.Result()
      print(self.__roll_number, "\t", self.__Name, "\t", self.__total_marks, "\t",          self.__percentage, "\t", self.__grade, "\t",
         self.__result)
def main():
   gc = grade_calculator()
   gc.setgrade_calculator()
   gc.showgrade_calculator()
if __name__ == "__main__":
   main()

Output

Running the above code gives us the following result −

Enter Roll Number: 3
Enter Name: raj
Enter 5 subjects marks:
Subject 1: 86
Subject 2: 75
Subject 3: 69
Subject 4: 55
Subject 5: 92
3    Kumar    377    75.4    A    PASS

Updated on: 04-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements