This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Introduction to the

The first three introduced the function module distribution of student management system, respectively introduced how to input student information, modify student information, delete student information, and how to display student information. This article is mainly to introduce how to find student information, and the student information for some simple sort, such as according to the student number sort or grades and other information sort.

Find the student information function module

How to find student information module function:

Start by defining a list file to prevent duplicate names. Then obtain the input student ID in the console, and compare the student ID information with the information in the disk file to see whether the corresponding student information can be found. If the corresponding student information can be found, output it according to the corresponding format and clear the list. If you do not find the corresponding student information, the input error will be displayed, please re-enter.

Concrete implementation:

  1. Write the function search () called 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('For search by ID please enter 1, for search by name please enter 2:')
            if mode == '1':
                id = input('Please enter the student ID:')
            elif mode == '2':
                name = input('Please enter student's name:')
            else:
                print('Your input is wrong, 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)

                    elifname ! =' ':
                        if d['name'] == name:
                            student_query.append(d)
            # display the query result
            show_student(student_query)
            # Clear the list
            student_query.clear()
            answer = input('Do you want to continue? y/n')
            if answer == 'y' or answer == 'Y':
                continue
            else:
                break
        else:
            print('Student information has not been saved yet')
            return
Copy the code
  1. Define the function show_student(query student) to display query results
def show_student(lst) :
    if len(lst) == 0:
        print('No student information found, no data displayed!! ')
        return
    # define the title display format
    format_title = '{:^6}\t{:^10}\t{:^8}\t{:^10}\t{:^8}\t{:^8}'  Format string form
    print(format_title.format('ID'.'name'.'English Scores'.'python results'.'Java performance'.'Overall score'))
    # Define the display format of the 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 the code

Query the total number of students module

How to query the total number of students

First, judge whether the file exists, count the number of student information saved in the student information file, actually judge the length of the list.

The specific implementation

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

def total() :  Count the total number of students
    if os.path.exists(filename):  Check 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 at present......... ')
Copy the code

Ordering module

How to implement the sorting function

This paper mainly introduces how to sort students’ information by their scores. It mainly sorts students’ information in ascending or descending order according to their English scores, Python scores, Java scores and total scores. First, read the information of the disk file, and then choose ascending or descending order, and then sort the student information.

The specific implementation

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

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('What you typed is wrong, please retype:')
        sort()
    mode = input('Please select sort (1. Sort by English, 2. Sort by Python, 3. Select * from 'Java'; select * from 'Java';)
    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 wrong. Please re-enter it.')
        sort()
    show_student(student_new)
Copy the code

conclusion

So far, the framework of the student management system has been built, the rest is how to achieve, the next chapter will introduce how to achieve the operation of the code, as well as part of the debugging function, I hope to help you.