-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentManagement.py
180 lines (148 loc) · 4.93 KB
/
StudentManagement.py
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
# Student Management System
"""
Fields :- ['roll', 'name', 'age', 'email', 'phone']
1. Add New Student
2. View Students
3. Search Student
4. Update Student
5. Delete Student
6. Quit
"""
import csv
# Define global variables
student_fields = ['roll', 'name', 'age', 'email', 'phone']
student_database = 'students.csv'
def display_menu():
print("--------------------------------------")
print(" Welcome to Student Management System")
print("---------------------------------------")
print("1. Add New Student")
print("2. View Students")
print("3. Search Student")
print("4. Update Student")
print("5. Delete Student")
print("6. Quit")
def add_student():
print("-------------------------")
print("Add Student Information")
print("-------------------------")
global student_fields
global student_database
student_data = []
for field in student_fields:
value = input("Enter " + field + ": ")
student_data.append(value)
with open(student_database, "a", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerows([student_data])
print("Data saved successfully")
input("Press any key to continue")
return
def view_students():
global student_fields
global student_database
print("--- Student Records ---")
with open(student_database, "r", encoding="utf-8") as f:
reader = csv.reader(f)
for x in student_fields:
print(x, end='\t |')
print("\n-----------------------------------------------------------------")
for row in reader:
for item in row:
print(item, end="\t |")
print("\n")
input("Press any key to continue")
def search_student():
global student_fields
global student_database
print("--- Search Student ---")
roll = input("Enter roll no. to search: ")
with open(student_database, "r", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
if len(row) > 0:
if roll == row[0]:
print("----- Student Found -----")
print("Roll: ", row[0])
print("Name: ", row[1])
print("Age: ", row[2])
print("Email: ", row[3])
print("Phone: ", row[4])
break
else:
print("Roll No. not found in our database")
input("Press any key to continue")
def update_student():
global student_fields
global student_database
print("--- Update Student ---")
roll = input("Enter roll no. to update: ")
index_student = None
updated_data = []
with open(student_database, "r", encoding="utf-8") as f:
reader = csv.reader(f)
counter = 0
for row in reader:
if len(row) > 0:
if roll == row[0]:
index_student = counter
print("Student Found: at index ", index_student)
student_data = []
for field in student_fields:
value = input("Enter " + field + ": ")
student_data.append(value)
updated_data.append(student_data)
else:
updated_data.append(row)
counter += 1
# Check if the record is found or not
if index_student is not None:
with open(student_database, "w", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerows(updated_data)
else:
print("Roll No. not found in our database")
input("Press any key to continue")
def delete_student():
global student_fields
global student_database
print("--- Delete Student ---")
roll = input("Enter roll no. to delete: ")
student_found = False
updated_data = []
with open(student_database, "r", encoding="utf-8") as f:
reader = csv.reader(f)
counter = 0
for row in reader:
if len(row) > 0:
if roll != row[0]:
updated_data.append(row)
counter += 1
else:
student_found = True
if student_found is True:
with open(student_database, "w", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerows(updated_data)
print("Roll no. ", roll, "deleted successfully")
else:
print("Roll No. not found in our database")
input("Press any key to continue")
while True:
display_menu()
choice = input("Enter your choice: ")
if choice == '1':
add_student()
elif choice == '2':
view_students()
elif choice == '3':
search_student()
elif choice == '4':
update_student()
elif choice == '5':
delete_student()
else:
break
print("-------------------------------")
print(" Thank you for using our system")
print("-------------------------------")