2021寒假python自学体系整理(Day13)

(2.2)总结学生信息管理系统

总结

感觉十几天过得很快,当初的C和C++都是以学生信息管理系统为结尾,没想到python也是。感觉脑阔还是涨涨的,以后还是要多动手做项目呀,(⊙_⊙),冲冲冲。
在这里插入图片描述

代码

# @file  :学生信息管理系统.py
# @Time  :2021/2/2 13:47
# @Author:蝉鸣遗夏
# @fun   :PyCharm

import os

filename = 'stu.txt'

def main():
    while True:
        menu()
        choice = int(input('please choose your function:'))
        if choice in [1,2,3,4,5,6,7,8]:
            if choice == 1:
                insert()
            elif choice == 2:
                search()
            elif choice == 3:
                delete()
            elif choice == 4:
                modify()
            elif choice == 5:
                sort()
            elif choice == 6:
                totalsum()
            elif choice == 7:
                show()
            elif choice == 8:
                answer = input('exit?y/n')
                if answer == 'y' or answer == 'Y':
                    print('Thanks for using!!!')
                    break
                else:
                    continue

def menu():
    print('*************************************************')
    print('\t\t\t\tStudent Manage System')
    print('\t\t\t\tFunction Menu')
    print('\t\t\t\t1.insert')
    print('\t\t\t\t2.search')
    print('\t\t\t\t3.delete')
    print('\t\t\t\t4.modify')
    print('\t\t\t\t5.sort')
    print('\t\t\t\t6.totalsum')
    print('\t\t\t\t7.show')
    print('\t\t\t\t8.exit')
    print('***************************************************')

def insert():
    stu_list = []
    while True:
        id = input('please input ID:')
        if not id:
            break
        name = input('please input name:')
        if not name:
            break

        try:
            Eng = int(input('English score:'))
            Py = int(input('Python score:'))
            Ja = int(input('Jave score:'))
        except:
            print('invalued,please input again')
            continue

        student = {'id':id,'name':name,'English':Eng,'Python':Py,'Java':Ja}
        stu_list.append(student)
        answer = input('continue insert?y/n\n')
        if answer == 'y' or answer == 'Y':
            continue
        else:
            break

    save(stu_list)
    print('insert successful')

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

def search():
    stu_id = []
    while True:
        id = ''
        name = ''
        if os.path.exists(filename):
            mode = input('search by ID input 1,search by name input 2:')
            if mode == '1':
                id = input('ID:')
            elif mode == '2':
                name = input('Name:')
            else:
                print('ERROR,please input again!!!')
                search()
            with open(filename,'r',encoding='utf-8') as rfile:
                stu = rfile.readlines()
                for item in stu:
                    d = dict(eval(item))
                    if id != '':
                        if d['id'] == id:
                            stu_id.append(d)
                    elif name != '':
                        if d['name'] == name:
                            stu_id.append(d)

            show_student(stu_id)
            stu_id.clear()
            answer = input('continue search?y/n\n')
            if answer == 'y' or answer == 'Y':
                continue
            else:
                break
        else:
            print('have not saved')
            return

def show_student(lst):
    if len(lst) == 0:
        print('no information!!!')
        return
    format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
    print(format_title.format('id','name','English','Python','Java','Sum'))

    format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\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'))
                                ))

def delete():
    while True:
        stu_id = input('please input the id what you wanna delete:')
        if stu_id != '':
            if os.path.exists(filename):
                with open(filename,'r',encoding='utf-8') as file:
                    stu_old = file.readlines()
            else:
                stu_old = []
            flag = False
            if stu_old:
                with open(filename,'w',encoding='utf-8') as wfile:
                    d = {}
                    for item in stu_old:
                        d = dict(eval(item))
                        if d['id'] != stu_id:
                            wfile.write(str(d)+'\n')
                        else:
                            flag = True
                    if flag:
                        print('deleting %s successful'%stu_id)
                    else:
                        print('no found %s'%stu_id)
            else:
                print('no student information')
                break
            show()
            answer = input('continue delete?y/n\n')
            if answer == 'y' or answer == 'Y':
                continue
            else:
                break

def modify():
    show()
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            stu_old = rfile.readlines()
    else:
        return
    stu_id = input('please input the ID what you wanna modify:')
    with open(filename,'w',encoding='utf-8') as wfile:
        for item in stu_old:
            d = dict(eval(item))
            if d['id'] == stu_id:
                print('Found successful,you can modify!')
                while True:
                    try:
                        d['name'] = input('Name:')
                        d['English'] = input('English:')
                        d['Python'] = input('Python:')
                        d['Java'] = input('Java:')
                    except:
                        print('ERROR,please input again!')
                    else:
                        break
                wfile.write(str(d)+'\n')
                print('modify successful!!!')
            else:
                wfile.write(str(d)+'\n')
        answer = input('continue modify?y/n\n')
        if answer == 'y' or answer == 'Y':
            modify()

def sort():
    show()
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            stu_list = rfile.readlines()
        stu_new = []
        for item in stu_list:
            d = dict(eval(item))
            stu_new.append(d)
    else:
        return
    asc_desc = input('please choose(0:asc,1:desc):')
    if asc_desc == '0':
        asc_desc_bool = False
    elif asc_desc == '1':
        asc_desc_bool = True
    else:
        print('ERROR,please input again:')
        sort()
    mode = input('please choose(1:English,2:Python,3:Java,4:Total):')
    if mode == '1':
        stu_new.sort(key=lambda x: int(x['English']),reverse=asc_desc_bool)
    elif mode == '2':
        stu_new.sort(key=lambda x: int(x['Python']), reverse=asc_desc_bool)
    elif mode == '3':
        stu_new.sort(key=lambda x: int(x['Java']), reverse=asc_desc_bool)
    elif mode == '4':
        stu_new.sort(key=lambda x: int(x['English']) + int(x['Python']) + int(x['Java']), reverse=asc_desc_bool)
    else:
        print('ERROR,please inut again:')
        sort()
    show_student(stu_new)

def totalsum():
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            stu = rfile.readlines()
            if stu:
                print('Totally %d students' % len(stu))
            else:
                print('have not insert')
    else:
        print('have not saved')

def show():
    stu_lst = []
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            stu =rfile.readlines()
            for item in stu:
                stu_lst.append(eval(item))
            if stu_lst:
                show_student(stu_lst)
    else:
        print('have not saved')

if __name__ == '__main__':
    main()

在这里插入图片描述

打包

D:\pythoncode>cd 学生信息管理系统

D:\pythoncode\学生信息管理系统>pyinstaller -F 学生信息管理系统.py
89 INFO: PyInstaller: 3.4
89 INFO: Python: 3.5.2
90 INFO: Platform: Windows-7-6.1.7601-SP1
92 INFO: wrote D:\pythoncode\学生信息管理系统\学生信息管理系统.spec
95 INFO: UPX is not available.
202 INFO: Extending PYTHONPATH with paths
['D:\\pythoncode\\学生信息管理系统', 'D:\\pythoncode\\学生信息管理系统']
204 INFO: checking Analysis
204 INFO: Building Analysis because Analysis-00.toc is non existent
205 INFO: Initializing module dependency graph...
211 INFO: Initializing module graph hooks...
216 INFO: Analyzing base_library.zip ...
6084 INFO: running Analysis Analysis-00.toc
6562 INFO: Caching module hooks...
6573 INFO: Analyzing D:\pythoncode\学生信息管理系统\学生信息管理系统.py
6593 INFO: Loading module hooks...
6594 INFO: Loading module hook "hook-xml.py"...
7028 INFO: Loading module hook "hook-encodings.py"...
7158 INFO: Loading module hook "hook-pydoc.py"...
7177 INFO: Looking for ctypes DLLs
7209 INFO: Analyzing run-time hooks ...
7219 INFO: Looking for dynamic libraries
7388 INFO: Looking for eggs
7388 INFO: Using Python library f:\python\python35.dll
7389 INFO: Found binding redirects:
[]
7414 INFO: Warnings written to D:\pythoncode\学生信息管理系统\build\学生信息管理系统\warn-学生信息管理系统.txt
7534 INFO: Graph cross-reference written to D:\pythoncode\学生信息管理系统\build\学生信息管理系统\xref-学生信息管理系统.html
7546 INFO: checking PYZ
7546 INFO: Building PYZ because PYZ-00.toc is non existent
7547 INFO: Building PYZ (ZlibArchive) D:\pythoncode\学生信息管理系统\build\学生信息管理系统\PYZ-00.pyz
8448 INFO: Building PYZ (ZlibArchive) D:\pythoncode\学生信息管理系统\build\学生信息管理系统\PYZ-00.pyz completed successfully.
8459 INFO: checking PKG
8460 INFO: Building PKG because PKG-00.toc is non existent
8460 INFO: Building PKG (CArchive) PKG-00.pkg
11283 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
11289 INFO: Bootloader f:\python\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
11289 INFO: checking EXE
11289 INFO: Building EXE because EXE-00.toc is non existent
11290 INFO: Building EXE from EXE-00.toc
11290 INFO: Appending archive to EXE D:\pythoncode\学生信息管理系统\dist\学生信息管理系统.exe
11327 INFO: Building EXE from EXE-00.toc completed successfully.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值