Skip to content

Instantly share code, notes, and snippets.

@OlivierJM
Last active February 8, 2024 17:55
Show Gist options
  • Save OlivierJM/26618937fc76c5d4d809779946ec7ad9 to your computer and use it in GitHub Desktop.
Save OlivierJM/26618937fc76c5d4d809779946ec7ad9 to your computer and use it in GitHub Desktop.
student management
import json
students = []
def add_student():
print("\nAdd New Student Details:")
last_name = input("Last Name: ")
first_name = input("First Name: ")
id_number = input("ID Number: ")
major = input("Major: ")
email = input("Email: ")
student = {
'last_name': last_name,
'first_name': first_name,
'id_number': id_number,
'major': major,
'email': email
}
students.append(student)
print("\nStudent added successfully!")
def remove_student():
last_name = input("Enter last name for student to be removed")
for student in students:
if student['last_name'] == last_name:
students.remove(student)
print(f"{last_name} removed successfully")
return
print(f"Student with last name '{last_name}' not found")
def modify_student():
print("Student modified")
def find_student():
last_name = input("Search by last name, type and press enter")
for student in students:
if student['last_name'] == last_name:
print(f"Student with last name '{last_name}' found")
# Show details
single_student_details(student)
print(f"Student with last name '{last_name}' not found")
def show_all_students():
print("\nList of all students:")
for student in students:
single_student_details(student)
def save_students_to_file():
file = open('students.json', 'w')
json.dump(students, file)
file.close()
print("\nStudent data saved successfully")
def read_students_file():
global students
try:
saved_file = open('students.json', 'r')
students = json.load(saved_file) # update the global student array to further read from it.
print("File found and data was auto added to students data")
except FileNotFoundError:
# wrong file name or file wasnt just found
print("File not found, try something else")
def single_student_details(student):
print("\nStudent Details:")
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'])
def entry_menu():
while True:
print("\nStudent Administration Office, Type a number that matches your desired action ")
print("1. Add Student")
print("2. Remove Student")
print("3. Modify Student")
print("4. Find Student")
print("5. Load students from file")
print("6. Save students to file")
print("7. Show all saved students")
print("8. Exit")
print()
choice = input("\nWhat do you want to do, (choice from 1-8): ")
if choice == '1':
add_student()
elif choice == '2':
remove_student()
elif choice == '3':
modify_student()
elif choice == '4':
find_student()
elif choice == '5':
read_students_file()
elif choice == '6':
save_students_to_file()
elif choice == '7':
show_all_students()
elif choice == '8':
break
else:
print("\nChoose only a number between 1 and 8.")
if __name__ == "__main__":
entry_menu()
import json
students = []
def add_student():
print("\nAdd New Student Details:")
last_name = raw_input("Last Name: ")
first_name = raw_input("First Name: ")
id_number = raw_input("ID Number: ")
major = raw_input("Major: ")
email = raw_input("Email: ")
student = {
'last_name': last_name,
'first_name': first_name,
'id_number': id_number,
'major': major,
'email': email
}
students.append(student)
print("\nStudent added successfully!")
def remove_student():
last_name = raw_input("Enter last name for student to be removed")
for student in students:
if student['last_name'] == last_name:
print("Student found, add new details")
add_student()
# After adding a new student then we remove the previous one
students.remove(student)
return
print("Student not found")
def modify_student():
last_name = raw_input("Enter last name for student to be modified")
for student in students:
if student['last_name'] == last_name:
print("Student found")
# Show details
single_student_details(student)
print("Student modified")
print("Student not found")
def find_student():
last_name = raw_input("Search by last name, type and press enter")
for student in students:
if student['last_name'] == last_name:
print("Student found")
# Show details
single_student_details(student)
print("Student not found")
def show_all_students():
print("\nList of all students:")
for student in students:
single_student_details(student)
def save_students_to_file():
file = open('students.json', 'w')
json.dump(students, file)
file.close()
print("\nStudent data saved successfully")
def read_students_file():
global students
saved_file = open('students.json', 'r')
students = json.load(saved_file)
print("File found and data was auto added to students data")
def single_student_details(student):
print("\nStudent Details:")
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'])
def entry_menu():
while True:
print("\nStudent Administration Office, Type a number that matches your desired action ")
print("1. Add Student")
print("2. Remove Student")
print("3. Modify Student")
print("4. Find Student")
print("5. Load students from file")
print("6. Save students to file")
print("7. Show all saved students")
print("8. Exit")
print()
choice = raw_input("\nWhat do you want to do, (choice from 1-8): ")
if choice == '1':
add_student()
elif choice == '2':
remove_student()
elif choice == '3':
modify_student()
elif choice == '4':
find_student()
elif choice == '5':
read_students_file()
elif choice == '6':
save_students_to_file()
elif choice == '7':
show_all_students()
elif choice == '8':
break
else:
print("\nChoose only a number between 1 and 8.")
if __name__ == "__main__":
entry_menu()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment