- Project Name: Student Marks Management System
- Programming Language: Python
- Affected File:
student_marks.py - Input File:
students.txt - Bug Report Type: Debugging and Troubleshooting
The Student Marks Management System reads student details and marks from students.txt. It calculates each student's total marks, average marks, and grade, then displays the results.
During testing, several logical, input-handling, and file-handling problems were identified in the original program.
Original code:
average = total / 4Problem:
Each student has five subject marks, but the total was divided by four. Therefore, the calculated average was incorrect.
Example:
Total = 436
Incorrect average = 436 / 4 = 109
Correct average = 436 / 5 = 87.2
Root Cause:
The divisor was hard-coded as 4 instead of using the actual number of marks.
Fixed code:
average = total / len(marks)Status: Fixed and tested.
Original code:
for i in range(len(students) - 1):Problem:
The loop stopped before the last student. As a result, the final student record was not displayed.
Root Cause:
Subtracting 1 from the list length caused the loop to exclude the last index.
Fixed code:
for student in students:Reason for Fix:
Iterating directly through the list processes every student record.
Status: Fixed and tested.
Original code:
if average > 90:
grade = "A"
elif average > 75:
grade = "B"
elif average > 60:
grade = "C"
else:
grade = "F"Problem:
Students with averages exactly equal to 90, 75, or 60 would not receive the intended grade.
Root Cause:
The program used the > operator instead of >=.
Fixed code:
if average >= 90:
grade = "A"
elif average >= 75:
grade = "B"
elif average >= 60:
grade = "C"
else:
grade = "F"Status: Fixed.
Problem:
If students.txt did not exist, the program could terminate with a FileNotFoundError.
Root Cause:
The file was opened without exception handling.
Fixed code:
try:
with open("students.txt", "r") as file:
# Read file contents
pass
except FileNotFoundError:
print("Error: students.txt was not found.")Result:
The program now displays a clear error message and continues safely.
Status: Fixed and tested.
Problem:
A record containing a value such as abc instead of a number could cause a ValueError.
Example input:
Kiran,85,abc,78,90,95
Root Cause:
The program attempted to convert every mark to an integer without handling conversion errors.
Fixed code:
try:
marks = [int(mark.strip()) for mark in data[1:]]
except ValueError:
print(f"Warning: Invalid marks on line {line_number}.")
continueResult:
The invalid record is skipped, and valid student records are still displayed.
Status: Fixed and tested.
Problem:
Marks below 0 or above 100 are invalid, but the original program did not validate the range.
Example input:
Ravi,85,105,78,90,95
Root Cause:
The program converted marks to integers but did not check whether they were within the allowed range.
Fixed code:
if any(mark < 0 or mark > 100 for mark in marks):
print(f"Warning: Marks out of range on line {line_number}.")
continueResult:
The invalid record is rejected, and the program continues processing valid records.
Status: Fixed and tested.
Problem:
A student record with fewer or more than five marks could cause incorrect processing.
Root Cause:
The original program did not verify the number of comma-separated fields.
Fixed code:
if len(data) != 6:
print(f"Warning: Invalid data on line {line_number}.")
continueExplanation:
Each valid record must contain:
- One student name
- Five subject marks
Therefore, every record must contain exactly six comma-separated fields.
Status: Fixed and tested.
Problem:
The original program opened the file without explicitly managing the file resource.
Root Cause:
The file was opened using:
file = open("students.txt", "r")Fixed code:
with open("students.txt", "r") as file:
# Read student records
passExplanation:
The with statement automatically closes the file after use, even if an error occurs.
Status: Fixed.
- Read and inspected the original Python code.
- Checked the average calculation manually.
- Compared the number of marks with the divisor.
- Tested the program with multiple student records.
- Identified that the final student was skipped.
- Corrected the loop condition.
- Reviewed the grade comparison operators.
- Added exception handling for missing files.
- Added validation for numeric marks.
- Added validation for marks between
0and100. - Added validation for malformed records.
- Refactored the program using functions and safe file handling.
- Ran the corrected program using:
py student_marks.py- Compared the actual output with the expected output.
- Removed temporary test records after testing.
| Test Case | Expected Result | Actual Result | Status |
|---|---|---|---|
| Three valid student records | All students displayed | All three students displayed | Passed |
| Average calculation | Correct average using five marks | Correct averages displayed | Passed |
| Last student display | Final student displayed | Ananya displayed | Passed |
| Missing input file | Clear error message | Missing-file warning displayed | Passed |
| Non-numeric mark | Invalid record skipped | Invalid-marks warning displayed | Passed |
| Mark greater than 100 | Invalid record skipped | Out-of-range warning displayed | Passed |
| Malformed record | Invalid record skipped | Invalid-data warning displayed | Passed |
| Clean input after testing | No warnings | Program ran without warnings | Passed |
The corrected program successfully:
- Calculates student averages correctly.
- Displays every student.
- Assigns grades using correct boundary conditions.
- Handles missing files safely.
- Detects invalid marks.
- Rejects marks outside the range
0–100. - Rejects malformed records.
- Uses safe file handling.
- Provides clear warning and error messages.
All identified bugs in the original Student Marks Management System were corrected and tested. The final program is more accurate, reliable, readable, and maintainable than the original version.