-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_manag
More file actions
193 lines (183 loc) · 6.42 KB
/
student_manag
File metadata and controls
193 lines (183 loc) · 6.42 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
import csv, os, shutil, datetime
# --------------------------
# Setup paths
BASE_DIR = r"C:\Users\Admin\Documents\python project"
os.makedirs(BASE_DIR, exist_ok=True)
STUDENTS_FILE = os.path.join(BASE_DIR, "students.csv")
DELETED_FILE = os.path.join(BASE_DIR, "students_deleted.csv")
FIELDNAMES = ['Roll_No','Name','Branch','Year','Gender','Age','Attendance_%','Mid1_Marks','Mid2_Marks','Quiz_Marks','Final_Marks']
# --------------------------
# Ensure CSV exists
if not os.path.exists(STUDENTS_FILE):
with open(STUDENTS_FILE, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=FIELDNAMES)
writer.writeheader()
print("Created empty students.csv ✅")
# --------------------------
# Helper functions
def load_students():
with open(STUDENTS_FILE, newline='', encoding='utf-8') as f:
return list(csv.DictReader(f))
def save_students(students):
with open(STUDENTS_FILE, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=FIELDNAMES)
writer.writeheader()
writer.writerows(students)
def backup_csv():
ts = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
target = os.path.join(BASE_DIR, f"students_backup_{ts}.csv")
shutil.copy(STUDENTS_FILE, target)
return target
def input_student():
students = load_students()
roll = input("Enter Roll No: ").strip()
if any(s["Roll_No"] == roll for s in students):
print("Duplicate Roll_No!")
return None
return {
"Roll_No": roll,
"Name": input("Name: ").strip(),
"Branch": input("Branch: ").strip(),
"Year": input("Year: ").strip(),
"Gender": input("Gender: ").strip(),
"Age": input("Age: ").strip(),
"Attendance_%": input("Attendance %: ").strip(),
"Mid1_Marks": input("Mid1 Marks: ").strip(),
"Mid2_Marks": input("Mid2 Marks: ").strip(),
"Quiz_Marks": input("Quiz Marks: ").strip(),
"Final_Marks": input("Final Marks: ").strip()
}
def grade(score):
try:
score = float(score)
except:
score = 0
pct = score / 400 * 100
if pct >= 85: return 'A'
if pct >= 70: return 'B'
if pct >= 50: return 'C'
return 'D'
# --------------------------
# Clerk menu
def clerk_menu():
while True:
print("\n--- CLERK MENU ---")
print("1. Add student")
print("2. Delete student")
print("3. Backup CSV")
print("4. Exit")
ch = input("Choice: ").strip()
students = load_students()
if ch == '1':
s = input_student()
if s:
students.append(s)
save_students(students)
print("Student added ✅")
elif ch == '2':
roll = input("Roll No to delete: ").strip()
found = [s for s in students if s["Roll_No"] == roll]
if found:
students = [s for s in students if s["Roll_No"] != roll]
save_students(students)
with open(DELETED_FILE, 'a', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=FIELDNAMES)
if f.tell() == 0:
writer.writeheader()
writer.writerow(found[0])
print("Student deleted ✅")
else:
print("Roll not found")
elif ch == '3':
path = backup_csv()
print("Backup created:", path)
elif ch == '4':
break
else:
print("Invalid choice")
# --------------------------
# Teacher menu
def teacher_menu():
while True:
print("\n--- TEACHER MENU ---")
print("1. Search student")
print("2. Update student marks/attendance")
print("3. Exit")
ch = input("Choice: ").strip()
students = load_students()
if ch == '1':
q = input("Search by Roll or Name: ").strip()
results = [s for s in students if s["Roll_No"] == q or q.lower() in s["Name"].lower()]
if results:
for s in results:
print(s)
else:
print("No student found")
elif ch == '2':
roll = input("Enter Roll No to update: ").strip()
for i, s in enumerate(students):
if s["Roll_No"] == roll:
field = input("Field to update: ").strip()
if field in FIELDNAMES:
students[i][field] = input("New value: ").strip()
save_students(students)
print("Updated ✅")
else:
print("Invalid field")
break
else:
print("Roll not found")
elif ch == '3':
break
else:
print("Invalid choice")
# --------------------------
# HOD menu
def hod_menu():
while True:
print("\n--- HOD MENU ---")
print("1. Generate report")
print("2. Exit")
ch = input("Choice: ").strip()
students = load_students()
if ch == '1':
totals = []
for s in students:
total = sum([float(s[f]) if s[f] else 0 for f in ['Mid1_Marks','Mid2_Marks','Quiz_Marks','Final_Marks']])
totals.append((s,total))
print(f"Total students: {len(totals)}")
for s,t in totals:
print(s["Roll_No"], s["Name"], t, grade(t))
# save report
report_file = os.path.join(BASE_DIR, "report.csv")
with open(report_file,'w',newline='',encoding='utf-8') as f:
w = csv.writer(f)
w.writerow(["Roll_No","Name","Total","Grade"])
for s,t in totals:
w.writerow([s["Roll_No"],s["Name"],t,grade(t)])
print("Report saved:", report_file)
elif ch == '2':
break
else:
print("Invalid choice")
# --------------------------
# Launcher
while True:
print("\n--- STUDENT MANAGEMENT SYSTEM ---")
print("Select your role:")
print("1. Clerk")
print("2. Teacher")
print("3. HOD")
print("4. Exit")
role = input("Enter choice: ").strip()
if role == '1':
clerk_menu()
elif role == '2':
teacher_menu()
elif role == '3':
hod_menu()
elif role == '4':
print("Exiting system. Goodbye!")
break
else:
print("Invalid choice")