Final examination in python -- student management system

Posted by purtip3154 on Wed, 05 Jan 2022 06:59:37 +0100

brief introduction

The first three times introduced the function module distribution of the student management system, respectively introduced how to enter student information, modify student information, delete student information, and how to display student information. This article mainly introduces how to find student information and sort the student information in some simple ways, such as sorting by student number or grade.

Find student information function module

How to realize the function of finding student information module:

First, define a list file to prevent duplicate names. Then get the input student ID on the console, compare the student ID information with the information in the disk file to see if the corresponding student information can be found. If the corresponding student information is found, output it according to the corresponding format and empty the list. If the corresponding student information is not found, the input error will be displayed. Please re-enter.

Specific implementation:

  1. Write the function called search in the main function to find student information.
def search():  # Find student information
    student_query = []  # Define a list file to prevent duplicate names
    while True:
        id = ''
        name = ''
        if os.path.exists(filename):
            mode = input('Press ID To find, enter 1, to find by name, enter 2:')
            if mode == '1':
                id = input('Please enter the student's ID: ')
            elif mode == '2':
                name = input('Please enter the student's name:')
            else:
                print('Your input is incorrect, please re-enter')
                search()
            with open(filename, 'r', encoding='utf-8') as rfile:
                student = rfile.readlines()
                for item in student:
                    d = dict(eval(item))
                    if id != '':
                        if d['id'] == id:
                            student_query.append(d)

                    elif name != '':
                        if d['name'] == name:
                            student_query.append(d)
            # Display query results
            show_student(student_query)
            # clear list 
            student_query.clear()
            answer = input('Continue query? y/n')
            if answer == 'y' or answer == 'Y':
                continue
            else:
                break
        else:
            print('Student information has not been saved yet')
            return
 Copy code
  1. Define the function show to display query results_ student(query student)
def show_student(lst):
    if len(lst) == 0:
        print('No student information found, no data displayed!!!')
        return
    # Define title display format
    format_title = '{:^6}\t{:^10}\t{:^8}\t{:^10}\t{:^8}\t{:^8}'  # Format the form of a string
    print(format_title.format('ID', 'full name', 'English achievement', 'python achievement', 'java achievement', 'Total score'))
    # Define the display format of content
    format_data = '{:^6}\t{:^10}\t{:^8}\t{:^10}\t{:^8}\t{:^8}'
    for item in lst:
        print(format_data.format(item.get('id'),
                                 item.get('name'),
                                 item.get('english'),
                                 item.get('python'),
                                 item.get('java'),
                                 int(item.get('english')) + int(item.get('python')) + int(item.get('java'))))
Copy code

Module for querying the total number of students

How to realize the function of querying the total number of students

First, judge whether the file exists and count the number of student information saved in the student information file. In fact, it is to judge the length of the list.

Concrete implementation

Write the function of the total number of students called in the main function Total ().

def total():  # Count the total number of students
    if os.path.exists(filename):  # Determine whether the file exists
        with open(filename, 'r', encoding='utf-8') as rfile:
            students = rfile.readlines()
            if students:
                print(f'Altogether{len(students)}Students')
            else:
                print('Student information has not been entered')
    else:
        print('Data not saved yet.........')
Copy code

Sorting module

How to implement sorting function

This paper mainly introduces how to sort student information through student achievement, and mainly sort student information in ascending or descending order according to English achievement, python achievement, java achievement and total achievement. First, read the information of the disk file, then select ascending or descending order, and then sort the student information.

Concrete implementation

Write the sorting function called sort () in the main function.

def sort():  # sort
    show()
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as rfile:
            student_list = rfile.readlines()
        student_new = []
        for item in student_list:
            d = dict(eval(item))
            student_new.append(d)
    else:
        return

    asc_or_desc = input('Please select (0 is ascending, 1 is descending):')
    if asc_or_desc == '0':
        asc_or_desc_bool = False
    elif asc_or_desc == '1':
        asc_or_desc = True
    else:
        print('Your input is incorrect, please re-enter:')
        sort()
    mode = input('Please select sorting method (1).Sorted by English, 2.according to python Sorting, 3.according to java Sorting, 4.Sort by total score):')
    if mode == '1':
        student_new.sort(key=lambda x: int(x['english']), reverse=asc_or_desc_bool)  # lambda anonymous function
    elif mode == '2':
        student_new.sort(key=lambda x: int(x['python']), reverse=asc_or_desc_bool)
    elif mode == '3':
        student_new.sort(key=lambda x: int(x['java']), reverse=asc_or_desc_bool)
    elif mode == '4':
        student_new.sort(key=lambda x: int(x['english'] + int(x['java']) + int(x['python'])), reverse=asc_or_desc_bool)
    else:
        print('Your input is incorrect. Please re-enter')
        sort()
    show_student(student_new)
Copy code

summary

So far, the large framework of the student management system has been built, and the rest is how to implement it. The next chapter will introduce how to run the code and some debugging functions, hoping to help you.

Topics: Python Back-end