-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent.py
More file actions
196 lines (175 loc) · 7.48 KB
/
student.py
File metadata and controls
196 lines (175 loc) · 7.48 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import json
def import_data_from_json(file_path):
global students
try:
with open(file_path, 'r') as file:
students = json.load(file)
print("Data imported successfully.")
except Exception as e:
print(f"Error importing data: {e}")
def register_the_student(student_id,student_name,student_batch):
if student_id in students:
print("Student ID already exists.")
return
students[student_id]={
"name": student_name,
"batch": student_batch,
"attendance": {
"total_days": 0,
"present_days": 0
},
"terms": {}
}
print(f"Student {student_name} registered successfully with ID {student_id}.")
def add_term_result(student_id, term_name, subject_marks_dict):
if student_id not in students:
print(f"Student {student_id} not found.")
return
students[student_id]["terms"][term_name] = subject_marks_dict
def record_attendance(student_id, total_days, present_days):
if student_id not in students:
print(f"Student {student_id} not found.")
return
students[student_id]["attendance"]["total_days"] += total_days
students[student_id]["attendance"]["present_days"] += present_days
def calculate_attendance_percentage(student_id):
if student_id not in students:
print(f"Student {student_id} not found.")
return 0
attendance = students[student_id]["attendance"]
if attendance["total_days"] == 0:
return 0
return (attendance["present_days"] / attendance["total_days"]) * 100
def get_topper_by_term(term_name):
top_student_id = None
top_student_name = None
top_avg = -1
for student_id, student_info in students.items():
if term_name in student_info["terms"]:
marks = student_info["terms"][term_name].values()
avg = sum(marks) / len(marks) if marks else 0
if avg > top_avg:
top_avg = avg
top_student_id = student_id
top_student_name = student_info["name"]
return top_student_name, top_avg
def generate_student_report(student_id):
if student_id in students:
students_info = students[student_id]
print(f"Student Report : {students_info['name']}({student_id})")
print(f"Batch: {students_info['batch']}")
attendance_percentage = calculate_attendance_percentage(student_id)
print(f"Attendance Percentage: {attendance_percentage:.2f}%")
print("Term Averages:")
overall_average = 0
cnt=0
for term, marks in students_info["terms"].items():
average_marks = sum(marks.values()) / len(marks) if marks else 0
print(f"{term}: {average_marks:.2f}")
overall_average += sum(marks.values()) / len(marks) if marks else 0
cnt+=1
print(f"Overall Average: {overall_average/cnt:.2f}")
else:
print(f"Student {student_id} not found.")
return
def rank_students_by_overall_average(batch):
ranked = []
for sid, info in students.items():
if info["batch"] == batch:
terms = info["terms"]
total = 0
count = 0
for marks in terms.values():
total += sum(marks.values())
count += len(marks)
avg = total / count if count > 0 else 0
ranked.append((sid, info["name"], avg))
ranked.sort(key=lambda x: x[2], reverse=True)
return ranked
def export_data_to_json(file_path):
try:
with open(file_path, 'w') as file:
json.dump(students, file, indent=4)
except Exception as e:
print(f"Error exporting data: {e}")
if __name__ == "__main__":
import_data_from_json("students.json")
print("Welcome to the Student Registration System")
print("CASE 1: Register a new student")
print("CASE 2 :ALREADY REGISTERED STUDENT ")
print("CASE 3 : GENERATE REPORT FOR STUDENT")
print("CASE 4 : GET TOP PERFORMER BY TERM")
print("CASE 5 : ADD TERM RESULTS")
print("CASE 6 : ADD ATTENDANCE")
print("CASE 7 : RANK STUDENTS BY OVERALL AVERAGE BY BATCH")
case_choice = input("Enter your choice : ")
if case_choice == "1":
student_id=input("Enter student ID to register: ")
student_name=input("Enter student name: ")
student_batch=input("Enter student batch: ")
register_the_student(student_id, student_name, student_batch)
elif case_choice == "2":
print("operation 1 : Add Term Results")
print("operation 2 : Add Attendance")
choice=input("Enter Your Operation Choice: ")
if choice =="1":
student_id=input("Enter student ID to add term results: ")
term_name=input("Enter term name: ")
subject_marks_dict = {}
subjects = ["Maths", "Science", "English"]
for subject in subjects:
subject_marks_dict[subject] = int(input(f"Enter marks for {subject}: "))
add_term_result(student_id, term_name, subject_marks_dict)
elif choice == "2":
student_id = input("Enter student ID to record attendance: ")
total_days = int(input("Enter total days: "))
present_days = int(input("Enter present days: "))
record_attendance(student_id, total_days, present_days)
elif case_choice == "3":
student_id = input("Enter student ID to generate report: ")
generate_student_report(student_id)
top_student=None
top_avg=float('-inf')
for sid in students:
terms=students[sid]["terms"]
tot=0
cnt=0
for marks in terms.values():
tot+=sum(marks.values())/len(marks) if marks else 0
cnt+=1
avg=tot/cnt if cnt>0 else 0
if avg >top_avg:
top_avg=avg
top_student=students[sid]["name"]
if top_student:
print(f"Top Performer: {top_student} with {top_avg:.2f} average")
elif case_choice == "4":
term_name = input(f"Enter term name to get top performer: ")
name, avg = get_topper_by_term(term_name)
if name:
print(f"Top Performer: {name} in {term_name} with {avg:.2f} average")
else:
print(f"No data found for term '{term_name}'.")
elif case_choice == "5":
student_id = input("Enter student ID to add term results: ")
term_name = input("Enter term name: ")
subject_marks_dict = {}
subjects = ["Maths", "Science", "English"]
for subject in subjects:
subject_marks_dict[subject] = int(input(f"Enter marks for {subject}: "))
add_term_result(student_id, term_name, subject_marks_dict)
elif case_choice == "6":
student_id = input("Enter student ID to add attendance: ")
total_days = int(input("Enter total days: "))
present_days = int(input("Enter present days: "))
record_attendance(student_id, total_days, present_days)
elif case_choice == "7":
batch = input("Enter batch to rank students: ")
ranked_students = rank_students_by_overall_average(batch)
if ranked_students:
print(f"Ranked Students in Batch {batch}:")
for rank, (sid, name, avg) in enumerate(ranked_students, start=1):
print(f"{rank}. {name} ({sid}) - Average: {avg:.2f}")
else:
print(f"No students found in batch '{batch}'.")
export_data_to_json("students.json")