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

Introduction to the

The last article introduced the structure and function of the management system in detail, and introduced how to achieve the function of inputting student information, this article will introduce how to delete student information and other specific module functions.

Introduction to main functions

  1. Before introducing the functions of each module, first of all, the description of the main function is introduced preliminarily:
def main() :  # main function
    while True:  # Always display the menu interface
        menm()
        choice = int(input('Please select:'))  Type conversion
        if choice in [0.1.2.3.4.5.6.7] :if choice == 0:
                answer = input('Are you sure you want to log out? y/n')
                if answer == 'y' or answer == 'Y':
                    print('Thank you for using!! ')
                    break  # Log out
                else:
                    continue
            elif choice == 1:
                insert()  # Input student information
            elif choice == 2:
                search()  # Find student information
            elif choice == 3:
                delect()  # Delete student information
            elif choice == 4:
                modify()  # Modify student information
            elif choice == 5:
                sort()  # sort
            elif choice == 6:
                total()  Count the total number of students
            elif choice == 7:
                show()  # Display all student information
Copy the code
  1. Define a menu display interface:
def menm() :  Define a function menu
    print('= = = = = = = = = = = = = = = = = the student information management system = = = = = = = = = = = = = = = = = = = = = = = = = = = =')
    print('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- function menu -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --')
    print('\t\t\t\t\ t1. Input student information ')
    print('\t\t\t\ t2. Find student information ')
    print('\t\t\t\t\ t3. Delete student information ')
    print('\t\t\t\ t4. Modify student information ')
    print('t \ \ t \ \ t \ \ t t t \ t5 sort')
    print('\t\t\t\t\t\ 6. Count the total number of students. ')
    print('\t\t\t\t\ t7 display all student information ')
    print('t \ \ t \ \ t \ \ t t t \ t0 exit')
    print('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --')
Copy the code

Through the main function call menu, understand the main implementation process and implementation steps of the program. And make clear that the management system has those modules and functions.

Delete student information

Input the student ID from the console, find the corresponding student information in the disk file, and delete it.

  1. Write the function delete () called in the main function to delete student information
def delect() :  # Delete student information
    while True:
        student_id = input('Please enter the ID of the student you want to delete:')
        ifstudent_id ! =' ':
            if os.path.exists(filename):  Check whether the disk file exists
                with open(filename, 'r', encoding='utf-8') as file:  Read the file as it exists
                    student_old = file.readlines()  Read all the data and put the data into the list
            else:
                student_old = []  Void if file does not exist

            flag = False  The default is not to delete
            if student_old:  # Judgment list
                with open(filename, 'w', encoding='utf-8') as wfile:  If there is data, open the file in write-only mode
                    d = {}
                    for item in student_old:
                        d = dict(eval(item))  # turn strings into dictionaries
                        if d['id'] != student_id:
                            wfile.write(str(d) + '\n')  Write the string to the file first
                        else:
                            flag = True  # indicates that it has been deleted
                    if flag:
                        print(F 'with id{student_id}The student has been removed ')  # f indicates a format string
                    else:
                        print(F prime was not found with ID{student_id}Student information ')
            else:  The disk has no data
                print('No Student information')
                break

            show()  Redisplay all students' information after deletion
            answer = input('Do you want to continue deleting? y/n')
            if answer == 'y' or answer == 'Y':
                continue
            else:
                break
Copy the code
  1. The show () function is called in the code to display student information, which will be completed later. The show function is used to realize the function of displaying student information.

Riding the wind and waves will sometimes, straight sail to the sea

There are not many things, I hope we can encourage each other. If you like, you can give the author a big praise, encourage it!!