Untitled
unknown
plain_text
5 months ago
7.0 kB
6
Indexable
import csv import os import ast students = {} data_file = 'students.csv' PREDEFINED_SUBJECTS = [ "Mathematics", "Physics", "Chemistry", "Biology", "History", "Geography", "Computer Science", "English Language", "Philosophy", "Sociology" ] def load_students(): if not os.path.exists(data_file): return with open(data_file, mode='r', newline='') as file: reader = csv.DictReader(file) for row in reader: student_id = int(row['id']) grades = ast.literal_eval(row['grades']) if row['grades'] else {} students[student_id] = { 'first_name': row['first_name'], 'last_name': row['last_name'], 'school_class': row['school_class'], 'address': row['address'], 'date_of_birth': row['date_of_birth'], 'contact': row['contact'], 'grades': grades } def save_students(): with open(data_file, mode='w', newline='') as file: fieldnames = ['id', 'first_name', 'last_name', 'school_class', 'address', 'date_of_birth', 'contact', 'grades'] writer = csv.DictWriter(file, fieldnames=fieldnames) writer.writeheader() for student_id, student in students.items(): student['grades'] = str(student['grades']) writer.writerow({ 'id': student_id, 'first_name': student['first_name'], 'last_name': student['last_name'], 'school_class': student['school_class'], 'address': student['address'], 'date_of_birth': student['date_of_birth'], 'contact': student['contact'], 'grades': student['grades'] }) def add_student_to_file(student): with open(data_file, mode='a', newline='') as file: fieldnames = ['id', 'first_name', 'last_name', 'school_class', 'address', 'date_of_birth', 'contact', 'grades'] writer = csv.DictWriter(file, fieldnames=fieldnames) writer.writerow({ 'id': student['id'], 'first_name': student['first_name'], 'last_name': student['last_name'], 'school_class': student['school_class'], 'address': student['address'], 'date_of_birth': student['date_of_birth'], 'contact': student['contact'], 'grades': str(student['grades']) }) def add_student_interactive(): while True: student_id = input("Enter student ID: ") if not student_id.isdigit(): print("Student ID must be a number.") continue student_id = int(student_id) if student_id in students: print(f"A student with ID {student_id} already exists. Please enter a different ID.") continue break first_name = input("Enter student's first name: ").capitalize() last_name = input("Enter student's last name: ").capitalize() school_class = input("Enter student's class (e.g., Grade 10): ") address = input("Enter student's address: ") date_of_birth = input("Enter student's date of birth (YYYY-MM-DD): ") contact = input("Enter student's contact number: ") grades = {} for subject in PREDEFINED_SUBJECTS: while True: grade_input = input(f"Enter grade for {subject} (or type 'skip' to leave blank): ") if grade_input.strip().lower() == 'skip': grades[subject] = None break try: grade = int(grade_input) if grade < 1 or grade > 10: print("Invalid grade. Please enter a grade between 1 and 10.") else: grades[subject] = grade break except ValueError: # This catches non-integer inputs print("Invalid grade. Please enter a whole number for the grade, or type 'skip' to leave blank.") student = { 'id': student_id, 'first_name': first_name, 'last_name': last_name, 'school_class': school_class, 'address': address, 'date_of_birth': date_of_birth, 'contact': contact, 'grades': grades } students[student_id] = student print(f"Added student: ID: {student_id}, Name: {first_name} {last_name}, Grades: {grades}") add_student_to_file(student) def update_grades(): student_id = int(input("Enter student ID to update: ")) subject = input("Enter subject for which to update the grade: ").strip() if subject not in PREDEFINED_SUBJECTS: print(f"Invalid subject: '{subject}'. Please check your spelling.") print(f"Available subjects: {', '.join(PREDEFINED_SUBJECTS)}") return if student_id not in students: print(f"Student with ID {student_id} not found.") return student = students[student_id] if subject in student['grades']: try: new_grade = int(input(f"Enter new grade for {subject}: ")) if 1 <= new_grade <= 10: student['grades'][subject] = new_grade print(f"Updated {student['first_name']} {student['last_name']}'s grade for {subject} to: {new_grade}") save_students() else: print("Invalid grade. Please enter a grade between 0 and 10.") except ValueError: print("Invalid input. Please enter a valid number for the grade.") else: print(f"No grade found for subject: {subject}") def view_students(): if not students: print("No students available.") return print("\nStudents List:") print("-" * 40) for student_id, student in students.items(): print(f"ID: {student_id}, Name: {student['first_name']} {student['last_name']}, Class: {student['school_class']}") print(f" Address: {student['address']}, Date of Birth: {student['date_of_birth']}, Contact: {student['contact']}") if isinstance(student['grades'], dict): for subject, grade in student['grades'].items(): print(f" {subject}: {grade}") else: print(" Grades data is corrupted. Expected dictionary, but found:", type(student['grades'])) print("-" * 40) def delete_student(): student_id = int(input("Enter student ID to delete: ")) if student_id in students: del students[student_id] print(f"Deleted student with ID: {student_id}") save_students() else: print(f"Student with ID {student_id} not found.") load_students() add_student_interactive() update_grades() view_students() delete_student()
Editor is loading...
Leave a Comment