Introduction
Result sheets are one of the first real problems every school or college automates. This program reads a list of subjects and one line per student, rejects bad lines with clear reasons, and prints a ranked result table followed by class statistics.
Problem statement
Objectives
- Model a record with a dataclass and computed properties
- Validate input line by line with exceptions
- Sort with a key function and compute competition ranks
- Format a readable table with f-strings
Features
- Any number of subjects
- Line-numbered rejection messages
- Shared ranks for equal totals (1, 2, 2, 4)
- Grades O, A+, A, B, C, D and F with a per-subject pass mark
- Pass rate, class average and subject toppers including ties
Concepts demonstrated
- Dataclasses and properties
- Input validation with ValueError
- Sorting with key functions
- Competition ranking
- Formatted output
System architecture
Student is a dataclass whose properties compute the total, percentage, pass status and grade from its marks. parse_student turns one line into a Student or raises ValueError with a specific message; main catches it and reports the line number. ranked sorts by total (descending) and roll number, giving equal totals the same rank. print_report prints the table and statistics using generator expressions over the students.
Folder structure
Complete source code
main.py
What each file does
- main.py
- The whole program: data classes, validation, the command loop and the report.
How to run it
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
Maths, Physics, Chemistry 101, Asha, 92, 88, 95 102, Ravi, 78, 30, 69 103, Meera, 85, 90, 80 104, Priya, 88, 82, 85 105, Kiran, 65, 72, 101 106, Arjun, seventy, 60, 55 103, Duplicate, 50, 50, 50 107, Sana, 40, 45 108, Vikram, 55, 61, 48
Testing
- Run
python main.py < tests/sample-input.txtand compare withtests/expected-output.txt. - Four lines are rejected: a mark of 101, the word 'seventy', a duplicate roll number 103 and a line with too few marks.
- Meera and Priya both total 255, so both are ranked 2 and Ravi is ranked 4.
- Ravi averages 59% but fails because Physics is 30, below the pass mark of 35.
Common errors
ValueError: invalid literal for int()
Check value.isdigit() before converting, as parse_student does, and report the bad field instead of crashing.
Percentages are wrong when a subject is added
Divide by len(self.marks) rather than a hard-coded number of subjects.
Possible improvements
- Read maximum marks per subject instead of assuming 100
- Export the result sheet to CSV with the csv module
- Add a grace-marks rule for students who miss the pass mark by one or two marks
Additional challenges
- 1Print each student's strongest and weakest subject.
- 2Show the median and standard deviation of percentages with the statistics module.
- 3Draw a text histogram of how many students got each grade.
Built and tested it yourself? Mark it to update your dashboard.