python student management system 1

Posted by b2k on Thu, 30 Dec 2021 08:05:08 +0100

I Enter student information

1. We first type out the options of the student management system, let's set the function step by step, and finally use it. At first I was like this:

def menm():
    print('====================Student information management system====================')
    print('--------------------------Management menu---------------------')
    print('\t\t\t\t\t\t1.Enter student information')
    print('\t\t\t\t\t\t2.Find student information')
    print('\t\t\t\t\t\t3.Delete student information')
    print('\t\t\t\t\t\t4.Modify student information')
    print('\t\t\t\t\t\t5.sort')
    print('\t\t\t\t\t\t6.Count the total number of students')
    print('\t\t\t\t\t\t7.Show all student information')
    print('\t\t\t\t\t\t0.sign out')
    print('------------------------------------------------------')

2. Here we set a menm function, which we want to call into our first step of circular selection:

def main():
    while True:
        menm()
        choice = int(input('Please select:'))
        if choice in [0,1,2,3,4,5,6,7]:
            if choice == 0:
                answer=input('Are you sure you want to exit the system? y/n\n')
                if answer == 'y' or answer=='Y':
                    print('Thank you for using!!!')
                    break
                else:
                    continue
            elif choice == 1:
                insert()#Enter student information
            elif choice==2:
                search()
            elif choice==3:
                delete()
            elif choice==4:
                modify()
            elif choice == 5:
                sort()
            elif choice == 6:
                total()
            elif choice == 7:
                 show()

3. Here, the program will make mistakes, so we define the above functions below, and then write pass. We will supplement them step by step.
4. Our mind map for inputting student information is as follows:

5. The above is our first two steps. Now let's complete the insert() function. This step is to enter student information:

def insert():
    student_list=[]
    while True:
        id = input('Please enter ID(E.g. 21262155):')
        if not id:
            break
        name = input('Please enter your name:')
        if not name:
            break

        try:
            english=int(input('Please enter your English score:'))
            python=int(input('Please enter python achievement:'))
            java = int(input('Please enter JAVA achievement:'))
        except:
            print('Invalid input, not an integer type. Please re-enter:')
            continue
        #Save the entered student information to the dictionary
        student={'id':id,'name':name,'english':english,'java':java}
        #Add student information to the list
        student_list.append(student)
        answer=input('Continue adding? y/n\n')
        if answer=='y':
            continue
        else:
            break

    #Call the save() function
    save(student_list)
    print('Student information entry completed!!!')
def save(lst):
    try:
        stu_txt=open(filename,'a',encoding='utf-8')
    except:
       stu_txt=open(filename,'w',encoding='utf-8')
    for item in lst:
        stu_txt.write(str(item)+'\n')
    stu_txt.close()

4. A function will be called, and then we must add filename = 'file name' at the beginning. If it is not read, an error will be reported; We read the contents of the file and share a mode of reading and writing the file:

II Delete student information

1. The mind map for deleting student information is:

2. In this step, we will complete the deletion of student information and complete our delete() function. Our code is as follows:

def delete():
   while True:
       student_id=input('Please enter the student to delete id:')
       if student_id !='':
           if os.path.exists(filename):
               with open(filename,'r',encoding='utf-8')as file:
                   student_old=file.readlines()
           else:
               student_old=[]
           flag=False #Mark whether to delete
           if student_old:
               with open(filename,'w',encoding='utf-8')as wfile:
                   d={}
                   for item in student_old:
                       d = dict(eval(item)) #Convert characters into Dictionaries
                       if d['id'] !=student_id:
                           wfile.write(str(d)+'\n')
                       else:
                           flag=True
                   if flag:
                       print('id by{student_id}Your student information has been deleted')
                   else:
                       print(f'Can't find ID by{student_id}Student information')
           else:
               print('No student information')
               break
           show()    #Redisplay all student information after deletion
           answer=input('Continue deletion? y/n\n')
           if answer=='y':
               continue
           else:
               break



III Modify student information

1. Revise the mind map of student information as follows:

2. In this step, we complete the content of the modification information. Here, we complete the modify() function as follows:

def modify():
    show()
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8')as rfile:
              student_old=rfile.readlines()
    else:
        return
    student_id=input('Please enter the name of the student to be modified ID:')
    with open(filename,'w',encoding='utf-8')as wfile:
        for item in student_old:
            d=dict(eval(item))
            if d['id']==student_id:
                print('Find the student information, you can modify his relevant information!')
                while True:
                    try:
                        d['name']=input('Please enter your name:')
                        d['english']=input('Please enter your English score:')
                        d['python']=input('Please enter python achievement:')
                        d['java']=input('Please enter java achievement:')
                    except:
                        print('Your input is wrong, please re-enter!!!')
                    else:
                        break
                wfile.write(str(d)+'\n')
                print('Modified successfully!!!')
            else:
                wfile.write(str(d)+'\n')
        answer=input('Continue to modify other student information? y/n\n')
        if answer=='y':
            modify()

2. Before this step, we need to introduce an import os into the initial code. Be sure to add it. Don't forget.

IV Find student information

1. The mind map for finding student information is:

2. We will complete the search() function written at the beginning. The code is as follows:

def  search():
    student_query=[]
    while True:
        id=''
        name=''
        if os.path.exists(filename):
            mode=input('Press ID Enter 1 to find and 2 to find by name:')
            if mode=='1':
                id=input('Please enter student ID:')
            elif mode=='2':
                name=input('Please enter student 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('Do you want to continue the query? y/n\n')
            if answer=='y':
                continue
            else:
                break
        else:
            print('Student information has not been saved yet')
            return
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{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^13}'
    print(format_title.format('ID','full name','English achievement','python achievement','Java achievement','Total score'))
    #Define content display format
    format_data = '{:^6}\t{:^4}\t{:^14}\t{:^10}\t{:^12}\t{:^10}'
    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'))
                                 ))
V Count the total number of students

1. The mind map for counting the total number of students is as follows:

2. In this step, we complete the total() function. The code is as follows:

def total():
    if os.path.exists(filename):
        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 information has not been saved yet!!!')

3. This step has the least code and should be well understood.

Vi Display all student information function

1. The mind map showing all student information functions is:

2. We complete the show() function. The code is as follows:

def show():
    student_lst=[]
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8')as rfile:
            students=rfile.readlines()
            for item in students:
                student_lst.append(eval(item))
            if student_lst:
                show_student(student_lst)
    else:
        print('Data has not been saved yet!!!')


3. Please look at the next blog. I will improve the whole student management system.

Topics: Python Back-end