Skip to content

Instantly share code, notes, and snippets.

@OlivierJM
Last active December 25, 2023 09:40
Show Gist options
  • Save OlivierJM/d4c2bf9806b981be668cc43ad7283344 to your computer and use it in GitHub Desktop.
Save OlivierJM/d4c2bf9806b981be668cc43ad7283344 to your computer and use it in GitHub Desktop.
records_keeper.py
import json
students_data = []
def add_student():
last_name = input("Enter last name: ")
first_name = input("Enter first name: ")
id_number = input("Enter ID number: ")
major = input("Enter major: ")
email = input("Enter email: ")
student = {
'last_name': last_name,
'first_name': first_name,
'id_number': id_number,
'major': major,
'email': email
}
students_data.append(student)
print("Student added successfully!")
def remove_student():
last_name = input("Enter last name to remove: ")
for student in students_data:
if student['last_name'] == last_name:
students_data.remove(student)
print("Student removed successfully!")
return
print("Student not found.")
def modify_student():
last_name = input("Enter last name to modify: ")
for student in students_data:
if student['last_name'] == last_name:
print("Current student details:")
print_student_details(student)
print("Enter new details:")
add_student()
students_data.remove(student)
print("Student modified successfully!")
return
print("Student not found.")
def search_student():
last_name = input("Enter last name to search: ")
for student in students_data:
if student['last_name'] == last_name:
print("Student found:")
print_student_details(student)
return
print("Student not found.")
def print_student_details(student):
print("Last Name:", student['last_name'])
print("First Name:", student['first_name'])
print("ID Number:", student['id_number'])
print("Major:", student['major'])
print("Email:", student['email'])
print()
def print_all_students():
print("List of all students:")
for student in students_data:
print_student_details(student)
def save_data_to_file():
with open('students_data.json', 'w') as file:
json.dump(students_data, file)
print("Data saved to file successfully!")
def load_data_from_file():
global students_data
try:
with open('students_data.json', 'r') as file:
students_data = json.load(file)
print("Data loaded from file successfully!")
except FileNotFoundError:
print("File not found. No data loaded.")
def main_menu():
while True:
print("\nStudent Administration Office")
print("1. Add Student")
print("2. Remove Student")
print("3. Modify Student")
print("4. Search Student")
print("5. Print All Students")
print("6. Save Data to File")
print("7. Load Data from File")
print("8. Exit")
choice = input("Enter your choice (1-8): ")
if choice == '1':
add_student()
elif choice == '2':
remove_student()
elif choice == '3':
modify_student()
elif choice == '4':
search_student()
elif choice == '5':
print_all_students()
elif choice == '6':
save_data_to_file()
elif choice == '7':
load_data_from_file()
elif choice == '8':
break
else:
print("Invalid choice. Please enter a number between 1 and 8.")
if __name__ == "__main__":
load_data_from_file() # Load existing data from file, if any
main_menu()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment