Skip to content
Extraits de code Groupes Projets
algorithme_gestion_etudiants.py 28,2 ko
Newer Older
  • Learn to ignore specific revisions
  • Adrien Payen's avatar
    Adrien Payen a validé
    import random
    import pandas as pd
    import os
    import statistics
    import re
    
    file_path = '/content/Data_Base.xlsx'
    data = pd.read_excel(file_path)
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
    excel_file_path = 'export_names.xlsx'
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    menu = []
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
    name_pattern = re.compile(r'^[A-Za-zéèêëàâäôöûüçÉÈÊËÀÂÄÔÖÛÜÇ][A-Za-zéèêëàâäôöûüçÉÈÊËÀÂÄÔÖÛÜÇ\'\-]*$')
    date_pattern = re.compile(r'^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\d{4}$')
    place_of_birth_pattern = re.compile(r'^[A-Za-zÀ-ÖØ-öø-ÿ\s]+$')
    address_pattern = re.compile(r'^[A-Za-z\s]+ \d+, [A-Za-z\s]+$')
    telephone_pattern = re.compile(r'^\d{3}/\d{2}\.\d{2}\.\d{2}$')
    email_pattern = re.compile(r'^[a-zA-Z0-9._%+-]+@student\.uclouvain\.be$')
    gender_pattern = re.compile(r'^[MFO]$')
    academic_year_pattern = re.compile(r'^(BAC[123]|MA[12])$')
    campus_pattern = re.compile(r'^(Louvain-la-Neuve|Mons)$', re.IGNORECASE)
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    
    # REGISTER
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def register_student(data):
        # Setting up the patterns to follow for entering information
    
        while True:
            Name = input("What is the student's name? ")  # Ask the user for the name
            if name_pattern.match(Name):  # Check if the name matches the specified format
                print("The name is valid.")
                break  # Exit the loop if the name is valid
    
    Adrien Payen's avatar
    Adrien Payen a validé
            else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
                print("The name is not valid. Make sure to follow the specified format.")
    
        while True:
            Surname = input("What is the student's surname? ")  # Ask the user for the surname
            if name_pattern.match(Surname):
                print("The surname is valid.")
                break  # Exit the loop if the surname is valid
    
    Adrien Payen's avatar
    Adrien Payen a validé
            else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
                print("The surname is not valid. Make sure to follow the specified format.")
    
        while True:
            Date_of_birth = input("Please enter a date in the format dd/mm/yyyy: ")
            if date_pattern.match(Date_of_birth):
                print("The date is valid.")
                break  # Exit the loop if the date is valid
    
    Adrien Payen's avatar
    Adrien Payen a validé
            else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
                print("The date is not valid. Make sure to follow the format dd/mm/yyyy.")
    
        while True:
            Place_of_birth = input("What is the city of birth? ")
            if place_of_birth_pattern.match(Place_of_birth):
                print("The city of birth is valid.")
                break  # Exit the loop if the city of birth is valid
    
    Adrien Payen's avatar
    Adrien Payen a validé
            else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
                print("The city of birth is not valid. Make sure to use only letters and spaces.")
    
        while True:
            Address = input("Please enter an address in the format 'street number, city': ")
            if address_pattern.match(Address):
                print("The address is valid.")
                break  # Exit the loop if the address is valid
    
    Adrien Payen's avatar
    Adrien Payen a validé
            else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
                print("The address is not valid. Make sure to follow the format 'street number, city'.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        while True:
            Telephone = input("What is the telephone number? (in the format 000/00.00.00) ")
            if telephone_pattern.match(Telephone):
                print("The telephone number is valid.")
                break  # Exit the loop if the telephone number is valid
            else:
                print("The telephone number is not valid. Make sure to follow the requested format.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        while True:
            Email = input("Enter the university email (@student.uclouvain.be): ")
            if email_pattern.match(Email):
                print("The email is valid.")
                break  # Exit the loop if the email is valid
            else:
                print("The email is not valid. Make sure to follow the requested format.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        while True:
            Gender = input("What is your gender? (For Male enter M, for Female enter F, and for another type enter O): ")
            if gender_pattern.match(Gender):
                print("The gender is valid.")
                break  # Exit the loop if the gender is valid
            else:
                print("The gender is not valid. Make sure to follow the requested format.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        while True:
            Academic_year = input("What is your academic year? (BAC1/BAC2/BAC3/MA1/MA2): ")
            if academic_year_pattern.match(Academic_year):
                print("The academic year is valid.")
                break  # Exit the loop if the academic year is valid
            else:
                print("The academic year is not valid. Make sure to follow the requested format.")
    
        Courses_and_grade = {}
        if Academic_year == 'MA2':
            for course in all_course_BAC1 + all_course_BAC2 + all_course_BAC3 + all_course_MA1 + all_course_MA2:
                grade = input(f"What is the grade for {course}? ")
                while not grade.isdigit() or int(grade) not in range(0, 21):
                    print("The grade must be an integer between 0 and 20.")
                    grade = input(f"What is the grade for {course}? ")
                Courses_and_grade[course] = int(grade)
        elif Academic_year == 'MA1':
            for course in all_course_BAC1 + all_course_BAC2 + all_course_BAC3 + all_course_MA1:
                grade = input(f"What is the grade for {course}? ")
                while not grade.isdigit() or int(grade) not in range(0, 21):
                    print("The grade must be an integer between 0 and 20.")
                    grade = input(f"What is the grade for {course}? ")
                Courses_and_grade[course] = int(grade)
        elif Academic_year == 'BAC3':
            for course in all_course_BAC1 + all_course_BAC2 + all_course_BAC3:
                grade = input(f"What is the grade for {course}? ")
                while not grade.isdigit() or int(grade) not in range(0, 21):
                    print("The grade must be an integer between 0 and 20.")
                    grade = input(f"What is the grade for {course}? ")
                Courses_and_grade[course] = int(grade)
        elif Academic_year == 'BAC2':
            for course in all_course_BAC1 + all_course_BAC2:
                grade = input(f"What is the grade for {course}? ")
                while not grade.isdigit() or int(grade) not in range(0, 21):
                    print("The grade must be an integer between 0 and 20.")
                    grade = input(f"What is the grade for {course}? ")
                Courses_and_grade[course] = int(grade)
        elif Academic_year == 'BAC1':
            for course in all_course_BAC1:
                grade = input(f"What is the grade for {course}? ")
                while not grade.isdigit() or int(grade) not in range(0, 21):
                    print("The grade must be an integer between 0 and 20.")
                    grade = input(f"What is the grade for {course}? ")
                Courses_and_grade[course] = int(grade)
    
    Adrien Payen's avatar
    Adrien Payen a validé
        while True:
            Campus = input("Enter 'Louvain-la-Neuve' or 'Mons' depending on the campus: ")
            if campus_pattern.match(Campus):
                print("The campus choice is valid.")
                break  # Exit the loop if the campus choice is valid
            else:
                print("The campus choice is not valid. Make sure to follow the requested format.")
    
        register(data, Name, Surname, Date_of_birth, Place_of_birth, Address, Telephone, Email, Gender, Academic_year, Courses_and_grade, Campus)
    
    
    def register(data, name, surname, date_of_birth, place_of_birth, address, telephone, email, gender, academic_year, courses_and_grade, campus):
        matricule = generate_matricule(name, surname, date_of_birth)
    
        student = {
            "Name": name,
            "Surname": surname,
            "Date of Birth": date_of_birth,
            "Place of Birth": place_of_birth,
            "Address": address,
            "Telephone": telephone,
            "Email": email,
            "Gender": gender,
            "Academic Year": academic_year,
            "Campus": campus,
            "Matricule": matricule
    
    Adrien Payen's avatar
    Adrien Payen a validé
        for course, grade in courses_and_grade.items():
            student[course] = grade
    
    Adrien Payen's avatar
    Adrien Payen a validé
        data = pd.concat([data, student], ignore_index=True)
    
    Adrien Payen's avatar
    Adrien Payen a validé
        data.to_excel(file_path, index=False)
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        print('The person has been registered.\n')
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def generate_matricule(name, surname, date_of_birth):
        consonants_name = ''.join([c for c in name if c.lower() not in 'aeiouy'])[:3]
        consonants_surname = ''.join([c for c in surname if c.lower() not in 'aeiouy'])[:2]
        last_consonant_surname = ''.join([c for c in surname if c.lower() not in 'aeiouy']).lower()[-1]  # not sure if it's the consonant of the first name or the last name
        birth_year_last_two_digits = str(date_of_birth)[-2:]
        random_integer = random.randint(0, 10)
        matricule_e = f"{consonants_name}{consonants_surname}{last_consonant_surname}{birth_year_last_two_digits}{random_integer}"
    
    Adrien Payen's avatar
    Adrien Payen a validé
        matricule_e = matricule_e.lower()
    
        return matricule_e
    
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def modify(data):
        while True:
            matricule_to_modify = input("Enter the matricule of the student you want to modify (or 'q' to quit): ")
            if matricule_to_modify.lower() == 'q':
                print("Operation canceled.")
                break
    
            student_index = data[data['Matricule'] == matricule_to_modify.lower()].index.tolist()
    
            if not student_index:
                print("No student found with the specified matricule.")
                return
    
            student_index = student_index[0]
            print(f"\nModifying the student with matricule {matricule_to_modify}:\n")
            print("1. Name")
            print("2. Surname")
            print("3. Date of Birth")
            print("4. Place of Birth")
            print("5. Address")
            print("6. Telephone")
            print("7. Email")
            print("8. Sex (M | F | O)")
            print("9. Academic Year")
            print("10. Curriculum")
            print("11. Courses already passed and their grade")
            print("12. Campus")
    
            field_to_modify = int(input("Enter the number of the field you want to modify: "))
    
            field_name_to_modify = field_mapping.get(field_to_modify, None)
            print(f"You are modifying the field: {field_name_to_modify}")
    
            if field_to_modify == 1:  # If the field to modify is the name (Name)
                while True:
                    name = input("What is the name you want to modify? ")
                    if name_pattern.match(name):
                        print("The name is valid.")
                        data.at[student_index, 'Name'] = name
                        break  # Exit the loop if the name is valid
                    else:
                        print("The name is not valid. Make sure to follow the specified format.")
            elif field_to_modify == 2:
                while True:  # Ask the user for the surname
                    surname = input("What is the surname? ")
                    if name_pattern.match(surname):
                        print("The surname is valid.")
                        data.at[student_index, 'Surname'] = surname
                        break  # Exit the loop if the surname is valid
                    else:
                        print("The surname is not valid. Make sure to follow the specified format.")
            elif field_to_modify == 3:
                while True:
                    date_of_birth = input("Please enter a date in the format dd/mm/yyyy: ")
                    if date_pattern.match(date_of_birth):
                        print("The date is valid.")
                        data.at[student_index, 'Date of Birth'] = date_of_birth
                        break  # Exit the loop if the date is valid
                    else:
                        print("The date is not valid. Make sure to follow the format dd/mm/yyyy.")
            elif field_to_modify == 4:
                while True:
                    place_of_birth = input("What is the city of birth? ")
                    if place_of_birth_pattern.match(place_of_birth):
                        print("The city of birth is valid.")
                        data.at[student_index, 'Place of Birth'] = place_of_birth
                        break  # Exit the loop if the city of birth is valid
                    else:
                        print("The city of birth is not valid. Make sure to use only letters and spaces.")
            elif field_to_modify == 5:
                while True:
                    address = input("Please enter an address in the format 'street number, city': ")
                    if address_pattern.match(address):
                        print("The address is valid.")
                        data.at[student_index, 'Address'] = address
                        break  # Exit the loop if the address is valid
                    else:
                        print("The address is not valid. Make sure to follow the format 'street number, city'.")
            elif field_to_modify == 6:
                while True:
                    telephone = input("What is the telephone number? (in the format 000/00.00.00) ")
                    if telephone_pattern.match(telephone):
                        print("The telephone number is valid.")
                        data.at[student_index, 'Telephone'] = telephone
                        break  # Exit the loop if the telephone number is valid
                    else:
                        print("The telephone number is not valid. Make sure to follow the requested format.")
            elif field_to_modify == 7:
                while True:
                    email = input("Enter the university email (@student.uclouvain.be): ")
                    if email_pattern.match(email):
                        print("The email is valid.")
                        data.at[student_index, 'Email'] = email
                        break  # Exit the loop if the email is valid
                    else:
                        print("The email is not valid. Make sure to follow the requested format.")
            elif field_to_modify == 8:
                while True:
                    gender = input("What is your gender? (For Male enter M, for Female enter F, and for other type enter O): ")
                    if gender_pattern.match(gender):
                        print("The gender is valid.")
                        data.at[student_index, 'Gender'] = gender
                        break  # Exit the loop if the gender is valid
                    else:
                        print("The gender is not valid. Make sure to follow the requested format.")
            elif field_to_modify == 9:
                while True:
                    academic_year = input("What is your academic year? (BAC1/BAC2/BAC3/MA1/MA2): ")
                    if academic_year_pattern.match(academic_year):
                        print("The academic year is valid.")
                        data.at[student_index, 'Academic Year'] = academic_year
                        break  # Exit the loop if the academic year is valid
                    else:
                        print("The academic year is not valid. Make sure to follow the requested format.")
            elif field_to_modify == 11:  # Courses and grades
                print("Courses already passed and their grade:\n")
                for course, grade in data.iloc[student_index].items():
                    if course not in field_mapping.values():
                        print(f"{course}: {grade}")
    
                while True:
                    course_to_modify = input("Enter the course you want to modify: ")
                    if course_to_modify in data.columns:  # The course has been found, you can continue with the rest of your code
                        print("The specified course has been found.")
                        break  # Exit the loop since the course has been found
                    else:
                        print("The specified course has not been found. Please select a new one.")
    
                new_grade = input(f"Enter the new grade for {course_to_modify}: ")
                while not new_grade.isdigit() or int(new_grade) not in range(0, 21):
                    print("The grade must be an integer between 0 and 20.")
                    new_grade = input(f"Enter the new grade for {course_to_modify}: ")
    
                # Update the grade
                data.at[student_index, course_to_modify] = int(new_grade)
            elif field_to_modify == 12:
                while True:
                    campus = input("Enter 'Louvain-la-Neuve' or 'Mons' depending on the campus: ")
                    if campus_pattern.match(campus):
                        print("The campus choice is valid.")
                        data.at[student_index, 'Campus'] = campus
                        break  # Exit the loop if the campus choice is valid
                    else:
                        print("The campus choice is not valid. Make sure to follow the requested format.")
            else:
                new_value = input(f"Enter the new value for the selected field: ")
                data.at[student_index, field_mapping[field_to_modify]] = new_value
    
    Adrien Payen's avatar
    Adrien Payen a validé
            # Save the modified data to the Excel file
            data.to_excel(file_path, index=False)
            print("Modification successfully done.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    # Mapping for field names to DataFrame column names
    
    Adrien Payen's avatar
    Adrien Payen a validé
    field_mapping = {
        1: 'Name',
        2: 'Surname',
        3: 'Date of Birth',
        4: 'Place of Birth',
        5: 'Address',
        6: 'Telephone',
        7: 'Email',
    
    Adrien Payen's avatar
    Adrien Payen a validé
        8: 'Gender',
    
    Adrien Payen's avatar
    Adrien Payen a validé
        9: 'Academic Year',
        10: 'Curriculum',
        12: 'Campus'
    }
    
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def delete(data):
        matricule_to_delete = input("Enter the matricule of the student you want to delete: ")
    
    Adrien Payen's avatar
    Adrien Payen a validé
        student_index = data[data['Matricule'] == matricule_to_delete.lower()].index.tolist()
    
        if not student_index:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print("No student found with the specified matricule.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            return
    
        student_index = student_index[0]
    
    Adrien Payen's avatar
    Adrien Payen a validé
        print(f"\nDeleting the student with matricule {matricule_to_delete}:\n")
    
    Adrien Payen's avatar
    Adrien Payen a validé
        # Display the details of the student before deletion
        print("Details of the student before deletion:")
    
    Adrien Payen's avatar
    Adrien Payen a validé
        print(data.iloc[student_index])
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        confirmation = input("Do you really want to delete this student? (YES/NO) ").upper()
        if confirmation == "YES":
            # Delete the student
    
    Adrien Payen's avatar
    Adrien Payen a validé
            data = data.drop(index=student_index)
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
            # Save the modified data to the Excel file
    
    Adrien Payen's avatar
    Adrien Payen a validé
            data.to_excel(file_path, index=False)
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print("Deletion successful.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
        else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print("Deletion canceled.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def find_student(data):
        search_criteria = input("Enter the search criteria (Name/Surname/Matricule): ").capitalize()
    
        if search_criteria == "Name":
            name_search = input("Enter the name of the student: ")
            results = data[data['Name'].str.contains(name_search, case=False, na=False)]
        elif search_criteria == "Surname":
            surname_search = input("Enter the surname of the student: ")
            results = data[data['Surname'].str.contains(surname_search, case=False, na=False)]
        elif search_criteria == "Matricule":
            matricule_search = input("Enter the matricule of the student: ")
            results = data[data['Matricule'].str.contains(matricule_search, case=False, na=False)]
    
    Adrien Payen's avatar
    Adrien Payen a validé
        else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print("Invalid search criteria.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            return
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        if results.empty:
            print("No student found with the specified criteria.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
        else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print("Student(s) found:")
            for index, row in results.iterrows():
                print(f"Name: {row['Name']}, Surname: {row['Surname']}, Matricule: {row['Matricule']}")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    # SHOW
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def filter_students(data):
    
    Adrien Payen's avatar
    Adrien Payen a validé
        if data.empty:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print("The list of students is empty.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
        else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print("\nList of students:")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            for index, row in data.iterrows():
    
    Adrien Payen's avatar
    Adrien Payen a validé
                print(f"Name: {row['Name']}, Surname: {row['Surname']}, Matricule: {row['Matricule']}")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    # Using the function export_filtered_student_list_to_excel
    # export_filtered_student_list_to_excel(data, 'filtered_student_list.xlsx')
    
    Adrien Payen's avatar
    Adrien Payen a validé
    print('To filter students, enter SCREEN')
    print('To export the list to an Excel document, enter EXPORT')
    print('To read the list, enter READ')
    choice = input("")
    if choice == "SCREEN":
        filter_students(data)
    elif choice == "EXPORT":
        export(data)
    elif choice == "READ":
        read(data)
    """
    
    def filter_students(data):
        show_list = input("Do you want to see the list of students? YES/NO")
        if show_list == "YES":
            for index, row in data.iterrows():
                print(f"Name: {row['Name']}, Surname: {row['Surname']}, Matricule: {row['Matricule']}")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            export_choice = input("Do you want to export the list? YES/NO")
    
            if export_choice == "YES":
                try:
                    data.to_excel(excel_file_path, index=False)
                    print(f"The list of students has been successfully exported to {excel_file_path}.")
                except Exception as e:
                    print(f"Error exporting to Excel: {e}")
    
                read_choice = input("Do you want to read the exported list? YES/NO")
    
                if read_choice == "YES":
                    try:
                        os.system(f"start excel {excel_file_path}")
                    except Exception as e:
                        print(f"Error opening the Excel file: {e}")
                        return open_excel(excel_file_path)
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
            return
        return
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def open_excel(excel_file_path):
    
    Adrien Payen's avatar
    Adrien Payen a validé
        try:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            os.system(f"start excel {excel_file_path}")
    
    Adrien Payen's avatar
    Adrien Payen a validé
        except Exception as e:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print(f"Error opening the Excel file: {e}")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    # SORTING
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def sort(data):
        print('1. Sort in ascending alphabetical order')
        print('2. Sort in descending alphabetical order')
        print('3. Sort by date of birth')
        print('4. Sort by age')
        print('5. Sort by matricule')
        print('6. Sort by academic year')
        print('7. Get all people who passed a course')
        print('8. Get all people who failed a course')
        print('9. Get all Bachelor students')
        print('10. Get all Master students')
        sorting_choice = input("")
        if sorting_choice == "1":
            sort_ascending(data)
        elif sorting_choice == "2":
            sort_descending(data)
        elif sorting_choice == "3":
            sort_by_date(data)
        elif sorting_choice == "4":
            sort_by_age(data)
        elif sorting_choice == "5":
            sort_by_matricule(data)
        elif sorting_choice == "6":
            sort_by_academic_year(data)
        elif sorting_choice == "7":
            sort_passed(data)
        elif sorting_choice == "8":
            sort_failed(data)
        elif sorting_choice == "9":
            sort_bachelor(data)
        elif sorting_choice == "10":
            sort_master(data)
        return
    
    def sort_ascending(data):
    
    Adrien Payen's avatar
    Adrien Payen a validé
        if 'Name' not in data.columns:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print("The 'Name' column does not exist in the DataFrame.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            return
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        # Sort the DataFrame by the 'Name' column
        sorted_data = data.sort_values(by='Name', ascending=True)
    
    Adrien Payen's avatar
    Adrien Payen a validé
        # Display the sorted list
        print("\nList of students sorted in alphabetical order (ascending):")
        for index, row in sorted_data.iterrows():
            print(f"Name: {row['Name']}, Surname: {row['Surname']}, Matricule: {row['Matricule']}")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    # Example usage
    # Replace 'data' with the name of your DataFrame
    
    Adrien Payen's avatar
    Adrien Payen a validé
    # STATS
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def statistics(data):
        print('1. Get basic statistics')
        print('2. Get all student grades')
        print('3. Get all grades for a course')
        print('4. Export all statistics')
    
    Adrien Payen's avatar
    Adrien Payen a validé
        while True:
            stats_choice = input("Enter the number of what you want to do: ")
            if stats_choice.isdigit():
                stats_int = int(stats_choice)
                if 1 <= stats_int <= 4:
                    print("The choice is valid.")
                    break
                else:
                    print("The choice must be one of those proposed.")
    
        if stats_choice == "1":
            basic_stats(data)
        elif stats_choice == "2":
            all_grades(data)
        elif stats_choice == "3":
            course_grades(data)
        elif stats_choice == "4":
            export_stats(data)
    
    def basic_stats(data):
        search_criteria = input("Enter the search criteria for student statistics (Name/Surname/Matricule): ").capitalize()
    
        if search_criteria == "Name":
            name_search = input("Enter the name of the student: ")
            results = data[data['Name'].str.contains(name_search, case=False, na=False)]
        elif search_criteria == "Surname":
            surname_search = input("Enter the surname of the student: ")
            results = data[data['Surname'].str.contains(surname_search, case=False, na=False)]
        elif search_criteria == "Matricule":
            matricule_search = input("Enter the matricule of the student: ")
            results = data[data['Matricule'].str.contains(matricule_search, case=False, na=False)]
    
    Adrien Payen's avatar
    Adrien Payen a validé
        else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print("Invalid search criteria.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            return
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        numeric_columns = results.select_dtypes(include=['number']).columns
    
    Adrien Payen's avatar
    Adrien Payen a validé
        for index, row in results.iterrows():
    
    Adrien Payen's avatar
    Adrien Payen a validé
            student_name = f"{row['Name']} {row['Surname']}"
    
    Adrien Payen's avatar
    Adrien Payen a validé
            student_grades = [row[column] for column in numeric_columns if not pd.isnull(row[column])]
    
            if student_grades:
                lowest_grade = min(student_grades)
                highest_grade = max(student_grades)
                average_grade = statistics.mean(student_grades)
                median_grade = statistics.median(student_grades)
                std_deviation = statistics.stdev(student_grades)
    
                print(f"\nStatistics for student {student_name}:")
                print(f"Lowest grade: {lowest_grade}")
                print(f"Highest grade: {highest_grade}")
                print(f"Average grade: {average_grade}")
                print(f"Median grade: {median_grade}")
                print(f"Standard deviation of grades: {std_deviation}")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
                print(f"\nNo grades found for student {student_name}.")
    
    def all_grades(data):
        search_criteria = input("Enter the search criteria for student grades (Name/Surname/Matricule): ").capitalize()
    
        if search_criteria == "Name":
            name_search = input("Enter the name of the student: ")
            results = data[data['Name'].str.contains(name_search, case=False, na=False)]
        elif search_criteria == "Surname":
            surname_search = input("Enter the surname of the student: ")
            results = data[data['Surname'].str.contains(surname_search, case=False, na=False)]
        elif search_criteria == "Matricule":
            matricule_search = input("Enter the matricule of the student: ")
            results = data[data['Matricule'].str.contains(matricule_search, case=False, na=False)]
    
    Adrien Payen's avatar
    Adrien Payen a validé
        else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            print("Invalid search criteria.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            return
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        student = data[data['Matricule'].str.contains(matricule_search, case=False, na=False)]
    
    Adrien Payen's avatar
    Adrien Payen a validé
        if student.empty:
            print(f"No student found with matricule {matricule_search}.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            return
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        # Select columns containing grades
        grade_columns = student.select_dtypes(include=['number']).columns
    
    Adrien Payen's avatar
    Adrien Payen a validé
        # Display student's grades
        print(f"Grades for student with matricule {matricule_search}:")
        for column in grade_columns:
            if not pd.isnull(student[column].values[0]):
                print(f"{column}: {student[column].values[0]}")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def course_grades(data):
    
    Adrien Payen's avatar
    Adrien Payen a validé
        numeric_columns = data.select_dtypes(include=['number']).columns.tolist()
        print("Here is the list of courses:")
        for column in numeric_columns:
            print(column)
    
    Adrien Payen's avatar
    Adrien Payen a validé
        course_name = input(print("For which course do you want to display grades?"))
    
    Adrien Payen's avatar
    Adrien Payen a validé
        if course_name not in data.columns:
            print(f"The specified course ({course_name}) was not found.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            return
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        # Filter students who participated in this course (non-null grade)
        students_in_course = data[data[course_name].notnull()]
    
    Adrien Payen's avatar
    Adrien Payen a validé
        if students_in_course.empty:
            print(f"No student participated in the course {course_name}.")
    
    Adrien Payen's avatar
    Adrien Payen a validé
            return
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        # Display grades of students for the specified course
        print(f"Grades of students for the course {course_name}:")
        for index, row in students_in_course.iterrows():
            print(f"Matricule: {row['Matricule']}, Grade: {row[course_name]}")
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def export_stats(data):
    
    Adrien Payen's avatar
    Adrien Payen a validé
        return
    
    Adrien Payen's avatar
    Adrien Payen a validé
    # Example of usage
    # Make sure you have defined and loaded your DataFrame 'data' before calling this function
    # Replace 'Course_Name' with the actual name of the course you are looking for
    # display_course_grades(data, 'Course_Name')
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    # Action
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
    def action():
        print("What do you want to do?\n Below, you will find what is possible followed by the commands to type.\n")
        print("1. Register a student")
        print("2. Modify one or more fields")
        print("3. Delete a student")
        print("4. Find a student")
        print("5. Show")
        print("6. Sort, display, or export the list")
        print("7. View statistics")
        print("8. Stop the program")
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        while True:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            command = input("Enter the number of what you want to do: ")  # Check if the command is an integer
            if command.isdigit():  # Convert the command to an integer
                command_int = int(command)  # Check if the command is between 1 and 8
                if 1 <= command_int <= 8:
                    print("The command is valid.")
                    break  # Exit the loop if the command is valid
                else:
                    print("The command must be one of those proposed.")
    
        if command == "1":
            register_student(data)
        elif command == "2":
            modify(data)
        elif command == "3":
            delete(data)
        elif command == "4":
            find_student(data)
        elif command == "5":
            show(data)
        elif command == "6":
            sort(data)
        elif command == "7":
            statistics(data)
        elif command == "8":
            return False
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    menu = []
    
    while True:
    
    Adrien Payen's avatar
    Adrien Payen a validé
        response = action()
        if response is False:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            break
        else:
    
    Adrien Payen's avatar
    Adrien Payen a validé
            if response == True:
                menu = []
            else:
                menu.append(response)