In this tutorial, we are going to build a Student Grading System for Nkumba University using Python and Object-Oriented Programming (OOP) principles. This guide is beginner-friendly and explains every concept in detail. By the end, you’ll understand not only how the code works, but why each part exists.
Our goal is not just to produce a working system, but to teach how and why each line of code is used so students can learn and apply these concepts on their own.
🎯 Problem Statement
Nkumba University needs a system that can automatically grade students based on their exam marks. The system should:
- Define a
Studentclass with name, registration number, course, and marks. - Include a function to compute grades using the scale:
- 80–100: Distinction
- 70–79: Very Good
- 60–69: Credit
- 50–59: Pass
- 0–49: Fail
- Validate that marks are between 0 and 100.
- Differentiate between undergraduate and postgraduate students using inheritance.
- Override the grading method for postgraduates if needed.
- Display each student’s details and grade.
💡 Concepts Covered
- Class and Object Creation
- Encapsulation
- Inheritance
- Polymorphism
- Input Validation
- Method Overriding
🧱 Step-by-Step Breakdown and Teaching Guide
1. Creating the Base Student Class
class Student:
def __init__(self, name, reg_no, course, marks):
self.name = name
self.reg_no = reg_no
self.course = course
self.marks = marks
What does this code mean?
class Student:defines a blueprint for all student objects. This is where we group related data and behavior.def __init__is a special method called a constructor. It runs automatically when we create a new student.self.name = name: This assigns the providednameto the object’s ownnameproperty.selfrefers to the object being created.- The same applies for
reg_no,course, andmarks.
2. Creating the Grade Calculator
def calculate_grade(self):
if 80 <= self.marks <= 100:
return "Distinction"
elif 70 <= self.marks < 80:
return "Very Good"
elif 60 <= self.marks < 70:
return "Credit"
elif 50 <= self.marks < 60:
return "Pass"
elif 0 <= self.marks < 50:
return "Fail"
else:
return "Invalid Marks"
Why is this here?
This function checks the student’s marks and returns a grade according to the university’s rules. We use if-elif-else statements to evaluate the correct grade range. The last else catches any values that are not valid.
3. Displaying the Student’s Information
def display(self):
print(f"Name: {self.name}")
print(f"Registration Number: {self.reg_no}")
print(f"Course: {self.course}")
print(f"Marks: {self.marks}")
print(f"Grade: {self.calculate_grade()}")
This method prints all the student’s details and their grade. Notice we call self.calculate_grade() to dynamically compute the grade based on the stored marks.
4. Extending to Postgraduate Students
class PostgraduateStudent(Student):
def calculate_grade(self):
if 85 <= self.marks <= 100:
return "Distinction"
elif 75 <= self.marks < 85:
return "Very Good"
elif 65 <= self.marks < 75:
return "Credit"
elif 55 <= self.marks < 65:
return "Pass"
elif 0 <= self.marks < 55:
return "Fail"
else:
return "Invalid Marks"
What does this mean?
class PostgraduateStudent(Student):means this class inherits fromStudent. It can reuse everything fromStudent, but also change what’s needed.calculate_grade()is overridden to use a different grading scale for postgraduate students. This is an example of polymorphism — the same method name does different things depending on the context.
5. Input Validation for Marks
def get_valid_marks():
while True:
try:
marks = float(input("Enter marks (0-100): "))
if 0 <= marks <= 100:
return marks
else:
print("Marks must be between 0 and 100.")
except ValueError:
print("Invalid input. Please enter a numeric value.")
This function ensures the user types a number, and that the number is between 0 and 100. We use a while True loop to keep asking until we get valid input.
try-excepthandles non-numeric entries gracefully.
6. Main Program and Final Code
if __name__ == "__main__":
print("\n--- Nkumba University Student Grading System ---")
name = input("Enter student name: ")
reg_no = input("Enter registration number: ")
course = input("Enter course: ")
level = input("Enter level (undergraduate/postgraduate): ").lower()
marks = get_valid_marks()
if level == "postgraduate":
student = PostgraduateStudent(name, reg_no, course, marks)
else:
student = Student(name, reg_no, course, marks)
print("\n--- Student Details ---")
student.display()
Why is this here?
if __name__ == "__main__":ensures the code only runs when this file is executed directly, not when imported elsewhere.- We collect the student’s name, registration number, course, level, and marks.
- Based on the level, we create an instance of either
StudentorPostgraduateStudent. student.display()prints everything, including the calculated grade.
✅ Summary and Key Learning Points
- You learned how to define classes in Python to group data and behavior.
- You used inheritance to avoid code duplication and handle specialized student types.
- You used method overriding and polymorphism to adjust behavior for postgraduates.
- You validated user input using
try-exceptblocks and loops. - You structured the program so it’s easy to read, test, and extend.
👨🏽🏫 Next Steps for Students
- Modify the program to handle multiple students at once.
- Store results in a text file or CSV.
- Add support for different grading systems by course.
- Turn this into a GUI application using Tkinter or PyQt.
1 Comment
Thanks😊for the good work