Winter vacation week summary

Main tasks of the week:

1.python learning

2. Winter vacation algorithm training

3. Database related content

1. I just finished learning the basics of python in one week

The following is a case of student management system:

Design module flow chart:

 

The following is the complete code:

import os
filename='student.txt'
def main():
    while True:
        menu()
        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')
                if answer=='y' or answer=='Y':
                    print('Thank you for your use!')
                    break
                else:
                    continue
            elif choice==1:
                insert()
            elif choice==2:
                search()
            elif choice == 3:
                delect()
            elif choice == 4:
                modify()
            elif choice == 5:
                sort()
            elif choice == 6:
                total()
            elif choice == 7:
                show()


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

def insert():
    student_list=[]
    while True:
        id=input('Please enter ID(For example: 01101 X)')
        if not id:
            break
        name=input('Please enter your name:')
        if not name:
            break
        try:
            English=int(input('Please enter your English score:'))
            Java = int(input('Please enter Java Achievement:'))
            python = int(input('Please enter python Achievement:'))
        except:
            print('Invalid entry / exit score, please re-enter:')
            continue
        #Save the information entered into the student in the dictionary
        student={'id':id,'name':name,'English':English,'Java':Java,'Python':python}
        #Add student list information to it
        student_list.append(student)
        answer=input('Continue adding? y/n')
        if answer=='y' or 'Y':
            continue
        else:
            break
    #Call the save() function
    sava(student_list)
    print('Student information entry completed!')

def sava(lis):
    try:
        stu_txt=open(filename,'a',encoding='utf-8')
    except:
        stu_txt=open(filename,'w',encoding='utf-8')
    for item in lis:
        stu_txt.write(str(item)+'\n')
        stu_txt.close()

def search():
    student_query=[]
    while True:
        id=''
        name=''
        if os.path.exists(filename):
            mode=input('Press ID Find input 1, find input 2 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 i in student:
                    d=dict(eval(i))
                    if id!='':
                        if['id']==id:
                            student_query.append(d)
                    elif name!='':
                        if['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')
            if answer=='y' or 'Y':
                continue
            else:
                break
        else:
            print('Student information has not been saved yet')
            return

def show_student(lis):
    if len(lis)==0:
        print('No student information is found and no data is displayed')
        return
    #Define title display format
    format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
    print(format_title.format('ID','full name','English achievement','Java achievement','python achievement','Total score'))
    #Define the display format of content
    format_data='{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}'
    for i in lis:
        print(format_data.format(i.get('id')),
                                 i.get('name'),
                                 i.get('English'),
                                 i.get('Java'),
                                 i.get('python'),
                                 int(i.get('English'))+int(i.get('Java'))+int(i.get('python')))

def delect():
    while True:
        student_id=input('Please enter the name of 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_id:
                with open(filename, 'w', encoding='utf-8') as wfile:
                    d={}
                    for item in student_old:
                        d=dict(eval(item)) #Convert string to dictionary
                        if d['id']!=student_id:
                            wfile.write(str(d)+'\n')
                        else:
                            flag=True
                    if flag:
                        print(f'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 student information after deletion
            answer=input('Continue deleting information? y/n')
            if answer=='y' or 'Y':
                continue
            else:
                break

def modify():
    show()
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as rfile:
            studendt_old=rfile.readlines()
    else:
        return
    studendt_id=input('Please enter the name of the student you want to modify id:')
    with open(filename, 'w', encoding='utf-8') as wfile:
        for i in studendt_old:
            d=dict(eval(i))
            if d['id']==studendt_id:
                print('Find the student information and modify it')
                while True:
                    try:
                        d['name']=input('Enter student 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 incorrect! Please re-enter:')
                    else:
                        break
                wfile.write(str(d)+'\n')
                print('Modification succeeded!')
            else:
                wfile.write(str(d) + '\n')
        answer=input('Enter additional student information? y/n')
        if answer=='y' or 'Y':
            modify()

def sort():
    show()
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as rfile:
            student_list=rfile.readlines()
        student_new=[]
        for i in student_list:
            d=dict(eval(i))
            student_new.append(d)
    else:
        return
    asc_or_desc=input('Please select sorting mode: 0-->Ascending, 1-->Descending order')
    if asc_or_desc=='0':
        asc_or_desc_bool=False
    elif asc_or_desc=='1':
        asc_or_desc_bool=True
    else:
        print('Your input is incorrect, please re-enter:')
        sort()
    mode=input('Please select sorting score: 1-->Sort by English score 2-->Press Java Grade ranking 3-->Press python Grade ranking 4-->Sort by total score')
    if mode=='1':
        student_new.sort(key=lambda student_new :int(['English']),reverse=asc_or_desc_bool)
    elif mode=='2':
        student_new.sort(key=lambda student_new :int(['Java']),reverse=asc_or_desc_bool)
    elif mode=='3':
        student_new.sort(key=lambda student_new :int(['python']),reverse=asc_or_desc_bool)
    elif mode=='4':
        student_new.sort(key=lambda student_new :int(['English'])+int(['Java'])+int(['python']),reverse=asc_or_desc_bool)
    else:
        print('Your input is incorrect, please re-enter')
        sort()
    show_student(student_new)

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('Data information has not been saved yet')
    else:
        print('Temporarily save student information for...')

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


if __name__ == '__main__':
        main()

 2. Winter vacation algorithm training and 3 Database related content

This week, I mainly studied the greedy algorithm and learned the concept of this algorithm, that is, "finding the maximum value under limited conditions",

The task of two questions a day has been written in previous blogs; Database related links are also available on the following blogs, which can be reviewed and consulted:

(101 messages) daily summary of winter vacation study D3_qq_53782756 blog - CSDN blog

(101 messages) daily summary of winter vacation study D4_qq_53782756 blog - CSDN blog

*** qq_ 53782756 blog - CSDN blog

Personal summary:

The first few days of this week are OK, but the situation is a little bad -#-!!! For their own reasons Therefore, through the tail of the weekend, make a promise to yourself, which can be regarded as isolating the bad mood in the past. Next semester must not fall in love, do a good job in learning and playing games!!! Wuhu!!!    ^_^

A few days ago, I mainly studied python. I arranged the contents of the database, did some algorithm problems, and learned about the relevant knowledge of Naoqi robot. More or less, I was careless, or I got a little lost. Next week, I mainly want to start to understand OpenVC, and the algorithm problem will be regarded as the content that I have to punch in every day (the Blue Bridge Cup 300 also makes me feel a little painful, but I still can't play water drift). In addition, if you have spare time, you have to understand java web. It seems very interesting There is also the design of Java GUI interface in network security competition!

What's more, you should criticize yourself! Learn to control yourself. Don't let the emotion at the moment affect the emotion and things in the future. Don't give up in case of difficulties. I believe others can. I can, but I learn later than others. In addition, we should seize the time to study in the University and keep up with the excellent seniors and sisters. When you encounter difficult (algorithm) problems, you should be patient with the code and learn other people's programming methods and good habits. Excellent code makes people feel beautiful, fast and streamlined.

Keywords: Python Back-end

Added by jmgrhm on Wed, 19 Jan 2022 14:03:31 +0200