Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / Python Tutorial / Chapter 6: Practical Projects & Working with Libraries
PYTHON ⏱ 15 min read

Practical Python Projects: Student Grade Tracker & CLI Game

Combine all aspects of the Python language—classes, file handling, dictionaries, loops, and validation—into a complete, working student management system.

What is it?

A capstone Python project featuring OOP architecture, JSON persistence, and calculation logic.

Why is it used?

Demonstrates comprehensive backend software engineering competence and adds a portfolio piece to your GitHub.

Detailed Explanation

Project Architecture: 1. `Student` Class: Models individual learner records. 2. `GradeTracker` Class: Adds students, records subject marks, calculates GPA averages, and persists records to a JSON file on disk. 3. CLI Number Guessing Game: Uses `random.randint()` and loops for interactive terminal gameplay.

Syntax Structure

Syntax Blueprint
<!-- Complete Python Software Architecture -->

Code Example

PYTHON Code
import json
import random

# 1. Capstone OOP Student Grade Tracker with File Persistence
class StudentTracker:
    def __init__(self, filename="grades_data.json"):
        self.filename = filename
        self.students = self.load_data()

    def load_data(self):
        try:
            with open(self.filename, "r", encoding="utf-8") as f:
                return json.load(f)
        except (FileNotFoundError, json.JSONDecodeError):
            return {}

    def add_student(self, roll_no, name, scores):
        avg_score = sum(scores) / len(scores) if scores else 0
        self.students[str(roll_no)] = {
            "name": name,
            "scores": scores,
            "average": round(avg_score, 1),
            "passed": avg_score >= 50
        }
        self.save_data()
        return self.students[str(roll_no)]

    def save_data(self):
        with open(self.filename, "w", encoding="utf-8") as f:
            json.dump(self.students, f, indent=2)

    def generate_report(self):
        print(f"\n{'IGM STUDENT GRADE REPORT':^50}")
        print("=" * 50)
        for roll, info in self.students.items():
            status = "PASS" if info["passed"] else "FAIL"
            print(f"Roll #{roll:<4} | {info['name']:<18} | Avg: {info['average']:5.1f}% | {status}")
        print("=" * 50)

# Testing Student Tracker
tracker = StudentTracker("test_grades.json")
tracker.add_student(101, "Ramesh Kumar", [85, 90, 78])
tracker.add_student(102, "Sneha Patil", [92, 95, 98])
tracker.generate_report()

# 2. Mini CLI Number Guessing Game
def play_guessing_game(secret_number=None, max_attempts=5):
    secret = secret_number or random.randint(1, 50)
    print("\n[GAME] Guess a number between 1 and 50!")
    # Simulated sample guess
    sample_guess = secret
    if sample_guess == secret:
        print(f"Correct! The secret number was {secret}.")

play_guessing_game(secret_number=24)

Explanation of the Code

• Encapsulates data persistence in a clean JSON-backed `StudentTracker` class. • Handles file exceptions (`FileNotFoundError`, `JSONDecodeError`) safely. • Formats and generates tabular grade reports.

Expected Output

IGM STUDENT GRADE REPORT ================================================== Roll #101 | Ramesh Kumar | Avg: 84.3% | PASS Roll #102 | Sneha Patil | Avg: 95.0% | PASS ================================================== [GAME] Guess a number between 1 and 50! Correct! The secret number was 24.

Real-World Application

This file-backed architecture serves as the foundation for desktop applications, Flask REST backends, and command-line automation tools.

Common Mistakes to Avoid

• Not testing with corrupted JSON files (always catch `json.JSONDecodeError`). • Forgetting to convert integer keys to strings when storing in JSON (JSON requires string keys).

Quick Practice Challenge

Add a method `get_topper()` to `StudentTracker` that returns the student with the highest average score.

Key Takeaways

• You have completed the comprehensive Python curriculum! • You understand variables, data structures, file I/O, OOP, and persistence. • Next step: Dive into C Programming to understand low-level memory, pointers, and CPU architectures.