As a programmer, I love building projects that solve real-world problems. One of the most common tasks in schools and colleges is grading students based on their marks. Today, I will walk you through a Python program that calculates a student’s grade based on their marks and enhances it with additional functionalities to make it more practical.
Understanding the Core Logic
The program follows a simple approach:
- Defining the Function (
grade_student
):- The function
grade_student(marks)
takes the student’s marks as input. - It uses conditional statements (
if-elif-else
) to determine the grade:- A for marks 90 and above.
- B for marks 80-89.
- C for marks 70-79.
- D for marks 60-69.
- E for marks 50-59.
- F for marks below 50.
- The function
- Taking User Input:
- The program asks the user to enter their marks, converts the input to a
float
, and stores it in a variable.
- The program asks the user to enter their marks, converts the input to a
- Calling the Function:
- The function
grade_student(marks)
is called, and the returned grade is stored in a variable.
- The function
- Displaying the Output:
- Finally, the program prints the student’s grade using an
f-string
.
- Finally, the program prints the student’s grade using an
Enhance the Program with More Practical Features
While the basic version of the program works, it lacks certain practical functionalities. Let’s improve it by adding:
Handling Multiple Students: Allow users to enter marks for multiple students. Validating Input: Prevent invalid entries (negative numbers, non-numeric input, etc.). Providing Performance Feedback: Offer personalized feedback for each grade. Calculating Class Average: Compute and display the class’s average marks and grade. Exit Option: Allow users to stop entering marks when they are done.
Python Code
# Function to determine grade and provide feedback
def grade_student(marks):
if marks >= 90:
return "A", "Excellent work! Keep it up!"
elif marks >= 80:
return "B", "Good job! Try to reach A next time."
elif marks >= 70:
return "C", "Fair performance. You can do better!"
elif marks >= 60:
return "D", "Needs improvement. Work harder!"
elif marks >= 50:
return "E", "Below average. Consider extra practice."
else:
return "F", "Fail. Study more and try again!"
# Main function to process multiple students
def main():
student_grades = [] # List to store marks
while True:
try:
marks = float(input("Enter the marks of the student (or type -1 to stop): "))
if marks == -1:
break # Exit loop
if marks < 0 or marks > 100:
print("Invalid input! Marks should be between 0 and 100.")
continue
grade, feedback = grade_student(marks)
student_grades.append(marks)
print(f"The grade of the student is: {grade}")
print(f"Feedback: {feedback}\n")
except ValueError:
print("Invalid input! Please enter a valid number.")
# Calculate and display the class average
if student_grades:
avg_marks = sum(student_grades) / len(student_grades)
avg_grade, _ = grade_student(avg_marks)
print(f"\nClass Average Marks: {avg_marks:.2f}")
print(f"Class Average Grade: {avg_grade}")
# Run the program
if __name__ == "__main__":
main()
New Features in This Version
✔ Handles Multiple Students: Users can enter marks for multiple students. ✔ Validates Input: Prevents invalid entries (negative marks, non-numeric input, etc.). ✔ Provides Feedback: Gives personalized feedback for each grade. ✔ Calculates Class Average: Computes and displays the average grade of all students. ✔ Exit Option: Allows users to stop entering marks by typing -1
.
Example Run
Enter the marks of the student (or type -1 to stop): 95
The grade of the student is: A
Feedback: Excellent work! Keep it up!
Enter the marks of the student (or type -1 to stop): 72
The grade of the student is: C
Feedback: Fair performance. You can do better!
Enter the marks of the student (or type -1 to stop): -1
Class Average Marks: 83.50
Class Average Grade: B
Why is This Better
Practical for teachers & students – Can be used for grading in classrooms. Real-world use case – Useful for educators, training programs, and assessments. Performance tracking – Helps analyze student performance trends.
Final Thoughts
Building a grading system in Python is a great beginner-friendly project that introduces core programming concepts such as functions, loops, conditionals, and input validation. By adding features like multiple student handling and class averages, we make the project more realistic and useful.