You are on page 1of 7

1 # Student Management System

2 """
3 Fields :- ['roll', 'name', 'age', 'email', 'phone']
4 1. Add New Student
5 2. View Students
6 3. Search Student
7 4. Update Student
8 5. Delete Student
9 6. Quit
10 """
11
12 import csv
13 # Define global variables
14 student_fields = ['roll', 'name', 'age', 'email', 'phone']
15 student_database = 'students.csv'
16
17
18 def display_menu():
19 print("--------------------------------------")
20 print(" Welcome to Student Management System")
21 print("---------------------------------------")
22 print("1. Add New Student")
23 print("2. View Students")
24 print("3. Search Student")
25 print("4. Update Student")
26 print("5. Delete Student")
27 print("6. Quit")
28
29
30 def add_student():
31 print("-------------------------")
32 print("Add Student Information")
33 print("-------------------------")
34 global student_fields
35 global student_database
36
37 student_data = []
38 for field in student_fields:
39 value = input("Enter " + field + ": ")
40 student_data.append(value)
41
42 with open(student_database, "a", encoding="utf-
43 8") as f:
44 writer = csv.writer(f)
45 writer.writerows([student_data])
46
47 print("Data saved successfully")
48 input("Press any key to continue")
49 return
50
51
52 def view_students():
53 global student_fields
54 global student_database
55
56 print("--- Student Records ---")
57
58 with open(student_database, "r", encoding="utf-
59 8") as f:
60 reader = csv.reader(f)
61 for x in student_fields:
62 print(x, end='\t |')
63 print("\n------------------------------------------------
64 -----------------")
65
66 for row in reader:
67 for item in row:
68 print(item, end="\t |")
69 print("\n")
70
71 input("Press any key to continue")
72
73
74 def search_student():
75 global student_fields
76 global student_database
77
78 print("--- Search Student ---")
79 roll = input("Enter roll no. to search: ")
80 with open(student_database, "r", encoding="utf-
81 8") as f:
82 reader = csv.reader(f)
83 for row in reader:
84 if len(row) > 0:
85 if roll == row[0]:
86 print("----- Student Found -----")
87 print("Roll: ", row[0])
88 print("Name: ", row[1])
89 print("Age: ", row[2])
90 print("Email: ", row[3])
91 print("Phone: ", row[4])
92 break
93 else:
94 print("Roll No. not found in our database")
95 input("Press any key to continue")
96
97
98 def update_student():
99 global student_fields
100 global student_database
101
102 print("--- Update Student ---")
103 roll = input("Enter roll no. to update: ")
104 index_student = None
105 updated_data = []
106 with open(student_database, "r", encoding="utf-
107 8") as f:
108 reader = csv.reader(f)
109 counter = 0
110 for row in reader:
111 if len(row) > 0:
112 if roll == row[0]:
113 index_student = counter
114 print("Student Found: at index
115 ",index_student)
116 student_data = []
117 for field in student_fields:
118 value = input("Enter " + field + ": ")
119 student_data.append(value)
120 updated_data.append(student_data)
121 else:
122 updated_data.append(row)
123 counter += 1
124
125
126 # Check if the record is found or not
127 if index_student is not None:
128
129 with open(student_database, "w",
130 encoding="utf-8") as f:
131 writer = csv.writer(f)
132 writer.writerows(updated_data)
133 else:
134 print("Roll No. not found in our database")
135
136 input("Press any key to continue")
137
138
139 def delete_student():
140 global student_fields
141 global student_database
142
143 print("--- Delete Student ---")
144 roll = input("Enter roll no. to delete: ")
145 student_found = False
146 updated_data = []
147 with open(student_database, "r", encoding="utf-
148 8") as f:
149 reader = csv.reader(f)
150 counter = 0
151 for row in reader:
152 if len(row) > 0:
153 if roll != row[0]:
154 updated_data.append(row)
155 counter += 1
156 else:
157 student_found = True
158
159 if student_found is True:
160
161 with open(student_database, "w",
162 encoding="utf-8") as f:
163 writer = csv.writer(f)
164 writer.writerows(updated_data)
165 print("Roll no. ", roll, "deleted successfully")
166 else:
167 print("Roll No. not found in our database")
168
169 input("Press any key to continue")
170
171 while True:
172 display_menu()
173
174 choice = input("Enter your choice: ")
175 if choice == '1':
176 add_student()
177 elif choice == '2':
178 view_students()
179 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("-------------------------------")

You might also like