-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_marks.py
More file actions
101 lines (73 loc) · 2.58 KB
/
Copy pathstudent_marks.py
File metadata and controls
101 lines (73 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Week 2 Task: Student Marks Management System
# Corrected and Refactored Version
def read_students():
"""Read student details from students.txt."""
students = []
try:
with open("students.txt", "r") as file:
for line_number, line in enumerate(file, start=1):
line = line.strip()
# Ignore empty lines
if not line:
continue
data = line.split(",")
# Each record must contain a name and 5 marks
if len(data) != 6:
print(f"Warning: Invalid data on line {line_number}.")
continue
name = data[0].strip()
try:
marks = [int(mark.strip()) for mark in data[1:]]
except ValueError:
print(f"Warning: Invalid marks on line {line_number}.")
continue
# Check that marks are within a valid range
if any(mark < 0 or mark > 100 for mark in marks):
print(f"Warning: Marks out of range on line {line_number}.")
continue
students.append({
"name": name,
"marks": marks
})
except FileNotFoundError:
print("Error: students.txt was not found.")
except OSError as error:
print(f"Error reading the file: {error}")
return students
def calculate_result(student):
"""Calculate total, average and grade for a student."""
marks = student["marks"]
if not marks:
return 0, 0, "N/A"
total = sum(marks)
average = total / len(marks)
if average >= 90:
grade = "A"
elif average >= 75:
grade = "B"
elif average >= 60:
grade = "C"
else:
grade = "F"
return total, average, grade
def display_results(students):
"""Display results for all students."""
print("\n===== STUDENT RESULTS =====")
if not students:
print("No valid student records found.")
return
for student in students:
total, average, grade = calculate_result(student)
print("Name:", student["name"])
print("Marks:", student["marks"])
print("Total:", total)
print("Average:", round(average, 2))
print("Grade:", grade)
print("---------------------------")
def main():
"""Run the Student Marks Management System."""
print("Student Marks Management System")
students = read_students()
display_results(students)
if __name__ == "__main__":
main()