functions

 avatar
unknown
plain_text
6 months ago
3.2 kB
6
Indexable
import pandas as pd
from datetime import datetime

# Globalne promenljive
students = {}
student_id_counter = 1
csv_file_path = 'students.csv'

# Fiksna lista predmeta
subjects = [
    "Mathematics",
    "Physics",
    "Chemistry",
    "Biology",
    "History",
    "Geography",
    "Computer Science",
    "English Language",
    "Philosophy",
    "Sociology"
]

def write_students_to_csv(students: dict) -> None:
    """Snimanje studenata u CSV datoteku."""
    try:
        df = pd.DataFrame.from_dict(students, orient='index')
        df.to_csv(csv_file_path, index_label='Student ID')
        print(f"Student data successfully saved to {csv_file_path}.")
    except Exception as e:
        print(f"An error occurred while saving: {e}")

def load_students() -> None:
    """Učitavanje studenata iz CSV datoteke."""
    global student_id_counter
    try:
        df = pd.read_csv(csv_file_path)
        df.columns = df.columns.str.strip()

        for _, row in df.iterrows():
            student_id = int(row.name)
            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': {}
            }
            student_id_counter = max(student_id_counter, student_id + 1)
    except FileNotFoundError:
        print("The file does not exist.")
    except pd.errors.EmptyDataError:
        print("The file is empty.")
    except Exception as e:
        print(f"An error occurred while loading: {e}")

def get_current_year():
    return datetime.now().year

def add_student(first_name: str, last_name: str, school_class: str, address: str, dob: str, contact: str) -> str:
    """Dodavanje novog studenta."""
    global students
    global student_id_counter

    school_class = f"{school_class}/{get_current_year()}"
    student_id = student_id_counter
    students[student_id] = {
        'first_name': first_name,
        'last_name': last_name,
        'school_class': school_class,
        'address': address,
        'date_of_birth': dob,
        'contact': contact,
        'grades': {}
    }

    write_students_to_csv(students)
    student_id_counter += 1
    return f"Student {first_name} {last_name} has been enrolled in class {school_class}."


def view_subjects():
    """Prikazivanje liste predmeta."""
    for subject in subjects:
        print(subject)


def update_grades_for_student(student_id: int, subject: str, new_grade: float) -> str:
    """Ažuriranje ocene za određenog studenta."""
    if subject not in subjects:
        raise ValueError(f"Subject '{subject}' is not in the fixed list of subjects.")

    if student_id in students:
        students[student_id]['grades'][subject] = new_grade
        write_students_to_csv(students)
        return f"Grade for subject '{subject}' has been successfully updated for student ID: {student_id}."
    else:
        raise ValueError("Student ID does not exist.")
Editor is loading...
Leave a Comment