-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacho.py
More file actions
265 lines (221 loc) · 9.16 KB
/
Copy pathMacho.py
File metadata and controls
265 lines (221 loc) · 9.16 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Udida Godswill
# def enroll_students():
# print()
# name = input("what is your name ? ")
# # print(name)
# while not name.strip():
# print("Invalid input. Please provide a name")
# while True:
# try:
# name = str(input("What is your name ? "))
# break
# except ValueError:
# print("Invalid input, please provide a valid name")
# print(f"Hello {name}!, Welcome to UDIS Console ")
# while True:
# try:
# age = int(input("How old are you ? "))
# break
# except ValueError:
# print("Invalid input. please enter a valid age.")
# print(f"Okay {name}, you are {age} years old. Is that correct?. Reply wiith Y for Yes and N for NO")
# reply = input().strip().lower()
# if reply == "y":
# print("Congrats, you have successfully been enrolled!")
# elif reply == "n":
# print("Okay, Please update your records.")
# else:
# print("Invalid reply. Enrollment process terminated.")
# enroll_students()
# Global list to store enrolled students
# enrolled_students = []
# def enroll_students():
# """Enroll a new student by asking for their name and age."""
# global enrolled_students
# # Ask for the user's name
# name = input("What is your name? ").strip()
# while not name:
# print("Invalid input. Please provide a valid name.")
# name = input("What is your name? ").strip()
# # Ask for the user's age
# while True:
# try:
# age = int(input("How old are you? ")) # Convert input to integer
# break
# except ValueError:
# print("Invalid input. Please enter a valid number for age.")
# # Confirm the details with the user
# print(f"Okay {name}, you are {age} years old. Is that correct? Reply Y/N")
# reply = input().strip().lower()
# if reply == "y":
# # Add the student to the global list
# details = {"name": name, "age": age}
# enrolled_students.append(details)
# print("Congrats, you have been successfully enrolled!")
# elif reply == "n":
# print("Okay, please restart the enrollment process.")
# else:
# print("Invalid reply. Enrollment process terminated.")
# def save_students_to_file(filename="students.txt"):
# """Save the list of enrolled students to a file."""
# try:
# with open(filename, "w") as file:
# file.write("Name Age\n") # Header
# for student in enrolled_students:
# file.write(f"{student['name']},{student['age']}\n")
# print(f"Enrolled students have been saved to {filename}.")
# except Exception as e:
# print(f"An error occurred while saving to file: {e}")
# def display_enrolled_students():
# """Display the list of currently enrolled students."""
# if not enrolled_students:
# print("No students have been enrolled yet.")
# else:
# print("\nList of Enrolled Students:")
# print("Name\tAge")
# for student in enrolled_students:
# print(f"{student['name']}\t{student['age']}")
# print()
# def main():
# while True:
# print("\nUDIS Console - Enrollment System")
# print("1. Enroll a Student")
# print("2. View Enrolled Students")
# print("3. Save Enrolled Students to File")
# print("4. Exit")
# choice = input("Choose an option (1-4): ").strip()
# if choice == "1":
# enroll_students()
# elif choice == "2":
# display_enrolled_students()
# elif choice == "3":
# save_students_to_file()
# elif choice == "4":
# print("Exiting the program. Goodbye!")
# break
# else:
# print("Invalid option. Please choose again.")
# # Run the program
# if __name__ == "__main__":
# main()
import os
class EnrollmentSystem:
def __init__(self):
"""Initialize the Enrollment System."""
self.enrolled_students = []
self.filename = "students.txt"
def load_students_from_file(self):
"""Load students from the file if it exists."""
if os.path.exists(self.filename):
try:
with open(self.filename, "r") as file:
for line in file.readlines()[1:]: # Skip header
name, age = line.strip().split(",")
self.enrolled_students.append({"name": name, "age": int(age)})
print(f"Loaded {len(self.enrolled_students)} students from {self.filename}.")
except Exception as e:
print(f"Error loading students from file: {e}")
def save_students_to_file(self):
"""Save the list of enrolled students to a file."""
try:
with open(self.filename, "w") as file:
file.write("Name,Age\n") # Header
for student in self.enrolled_students:
file.write(f"{student['name']},{student['age']}\n")
print(f"Enrolled students have been saved to {self.filename}.")
except Exception as e:
print(f"An error occurred while saving to file: {e}")
def enroll_student(self):
"""Enroll a new student."""
name = self._get_valid_name()
age = self._get_valid_age()
print(f"Okay {name}, you are {age} years old. Is that correct? Reply Y/N")
reply = input().strip().lower()
if reply == "y":
self.enrolled_students.append({"name": name, "age": age})
print("Congrats, you have been successfully enrolled!")
elif reply == "n":
print("Okay, please restart the enrollment process.")
else:
print("Invalid reply. Enrollment process terminated.")
def display_students(self):
"""Display the list of currently enrolled students."""
if not self.enrolled_students:
print("No students have been enrolled yet.")
else:
print("\nList of Enrolled Students:")
print(f"{'Name':<20}{'Age':<5}") # Table headers
print("-" * 25)
for student in self.enrolled_students:
print(f"{student['name']:<20}{student['age']:<5}")
print()
def search_student(self):
"""Search for a student by name."""
search_name = input("Enter the name of the student to search for: ").strip()
results = [s for s in self.enrolled_students if search_name.lower() in s['name'].lower()]
if results:
print("\nSearch Results:")
print(f"{'Name':<20}{'Age':<5}")
print("-" * 25)
for student in results:
print(f"{student['name']:<20}{student['age']:<5}")
else:
print(f"No students found with the name '{search_name}'.")
def delete_student(self):
"""Delete a student by name."""
name_to_delete = input("Enter the name of the student to delete: ").strip()
for student in self.enrolled_students:
if student['name'].lower() == name_to_delete.lower():
self.enrolled_students.remove(student)
print(f"Student {name_to_delete} has been removed.")
return
print(f"No student found with the name '{name_to_delete}'.")
def _get_valid_name(self):
"""Prompt the user for a valid name."""
name = input("What is your name? ").strip()
while not name:
print("Invalid input. Please provide a valid name.")
name = input("What is your name? ").strip()
return name
def _get_valid_age(self):
"""Prompt the user for a valid age."""
while True:
try:
age = int(input("How old are you? "))
if age <= 0:
raise ValueError
return age
except ValueError:
print("Invalid input. Please enter a valid positive number for age.")
def run(self):
"""Run the main menu of the enrollment system."""
self.load_students_from_file()
while True:
print("\nUDIS Console - Enrollment System")
print("1. Enroll a Student")
print("2. View Enrolled Students")
print("3. Search for a Student")
print("4. Delete a Student")
print("5. Save Enrolled Students to File")
print("6. Exit")
choice = input("Choose an option (1-6): ").strip()
if choice == "1":
self.enroll_student()
elif choice == "2":
self.display_students()
elif choice == "3":
self.search_student()
elif choice == "4":
self.delete_student()
elif choice == "5":
self.save_students_to_file()
elif choice == "6":
print("Exiting the program. Goodbye!")
self.save_students_to_file() # Save on exit
break
else:
print("Invalid option. Please choose again.")
# Run the program
if __name__ == "__main__":
system = EnrollmentSystem()
system.run()