-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschoolsearch.py
More file actions
125 lines (107 loc) · 5.33 KB
/
schoolsearch.py
File metadata and controls
125 lines (107 loc) · 5.33 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def load_students(file_path):
"""Load student data from the given file."""
students = []
try:
with open(file_path, 'r') as file:
for line in file:
fields = line.strip().split(',')
if len(fields) != 8:
print(f"Invalid line: {line.strip()}") # Debugging line
raise ValueError("Invalid file format.")
student = {
'last_name': fields[0],
'first_name': fields[1],
'grade': int(fields[2]),
'classroom': int(fields[3]),
'bus': int(fields[4]),
'gpa': float(fields[5]),
'teacher_last_name': fields[6],
'teacher_first_name': fields[7]
}
students.append(student)
return students
except FileNotFoundError:
print("Error: File not found.")
exit(1)
except ValueError as e:
print(f"Error: {e}")
exit(1)
def search_students_by_last_name(students, last_name, bus=False):
results = []
for student in students:
if student['last_name'] == last_name:
if bus:
results.append(f"{student['last_name']}, {student['first_name']}, Bus Route: {student['bus']}")
else:
results.append(f"{student['last_name']}, {student['first_name']}, Grade: {student['grade']}, Classroom: {student['classroom']}, Teacher: {student['teacher_last_name']} {student['teacher_first_name']}")
return results
def search_students_by_teacher(students, teacher_last_name):
return [f"{s['last_name']}, {s['first_name']}" for s in students if s['teacher_last_name'] == teacher_last_name]
def search_students_by_bus(students, bus_route):
return [f"{s['last_name']}, {s['first_name']}, Grade: {s['grade']}, Classroom: {s['classroom']}" for s in students if s['bus'] == bus_route]
def search_students_by_grade(students, grade):
return [f"{s['last_name']}, {s['first_name']}" for s in students if s['grade'] == grade]
def grade_average_gpa(students, grade):
grade_students = [s['gpa'] for s in students if s['grade'] == grade]
if grade_students:
return round(sum(grade_students) / len(grade_students), 2)
return None
def grade_high_low_gpa(students, grade, high=True):
grade_students = [s for s in students if s['grade'] == grade]
if not grade_students:
return None
key_func = max if high else min
student = key_func(grade_students, key=lambda s: s['gpa'])
return f"{student['last_name']}, {student['first_name']}, GPA: {student['gpa']}, Teacher: {student['teacher_last_name']} {student['teacher_first_name']}, Bus: {student['bus']}"
def info_summary(students):
grade_counts = {grade: 0 for grade in range(7)}
for student in students:
grade_counts[student['grade']] += 1
return [f"Grade {grade}: {count} Students" for grade, count in sorted(grade_counts.items())]
def main():
file_path = "students.txt"
students = load_students(file_path)
while True:
command = input("Enter command: ").strip()
if command.startswith('S:') or command.startswith('Student:'):
parts = command.split()
last_name = parts[1]
bus = len(parts) > 2 and (parts[2] == 'B' or parts[2] == 'Bus')
results = search_students_by_last_name(students, last_name, bus)
print("\n".join(results) if results else "No students found.")
elif command.startswith('T:') or command.startswith('Teacher:'):
teacher_last_name = command.split(':', 1)[1].strip()
results = search_students_by_teacher(students, teacher_last_name)
print("\n".join(results) if results else "No students found.")
elif command.startswith('B:') or command.startswith('Bus:'):
bus_route = int(command.split(':', 1)[1].strip())
results = search_students_by_bus(students, bus_route)
print("\n".join(results) if results else "No students found.")
elif command.startswith('G:') or command.startswith('Grade:'):
parts = command.split()
grade = int(parts[1])
if len(parts) > 2:
if parts[2] == 'H' or parts[2] == 'High':
result = grade_high_low_gpa(students, grade, high=True)
elif parts[2] == 'L' or parts[2] == 'Low':
result = grade_high_low_gpa(students, grade, high=False)
else:
result = "Invalid command."
print(result if result else "No students found.")
else:
results = search_students_by_grade(students, grade)
print("\n".join(results) if results else "No students found.")
elif (command.startswith('A:') or command.startswith('Average:')):
parts = command.split(':')
grade = int(parts[1].strip())
avg = grade_average_gpa(students, grade)
print(f"Grade {grade} Average GPA: {avg}" if avg is not None else "No students found.")
elif command == 'I' or command == 'Info':
summary = info_summary(students)
print("\n".join(summary))
elif command == 'Q' or command == 'Quit':
break
else:
print("Invalid command.")
if __name__ == "__main__":
main()