首页 > 编程语言 >python学习-学生信息管理系统并打包exe

python学习-学生信息管理系统并打包exe

时间:2023-04-22 16:45:52浏览次数:38  
标签:exe python else item student input print 信息管理系统 id

在B站自学Python
站主:Python_子木
授课:杨淑娟
平台: 马士兵教育
python: 3.9.9

python打包exe文件

#安装PyInstaller
pip install PyInstaller
#-F打包exe文件,stusystem\stusystem.py到py的路径,可以是绝对路径,可以是相对路径
pyinstaller -F stusystem\stusystem.py

学生信息管理系统具体代码如下

import os.path
filename='student.txt'
def main():
    while True:
        menu()
        choice = int(input('请选择'))
        if choice in range(8):
            if choice==0:
                answer=input('您确定要退出系统吗?y/n')
                if answer.lower()=='y':
                    print('谢谢您的使用!')
                    break #退出系统
                else:
                    continue
            if choice==1:
                insert()
            if choice==2:
                search()
            if choice==3:
                delete()
            if choice==4:
                modify()
            if choice==5:
                sort()
            if choice==6:
                total()
            if choice==7:
                show()
        else:
            print('无此功能,请按菜单重新选择')
def menu():
    print('=========================学生信息管理系统============================')
    print('-----------------------------功能菜单------------------------------')
    print('\t\t\t\t\t\t1.录入学生信息')
    print('\t\t\t\t\t\t2.查找学生信息')
    print('\t\t\t\t\t\t3.删除学生信息')
    print('\t\t\t\t\t\t4.修改学生信息')
    print('\t\t\t\t\t\t5.排序')
    print('\t\t\t\t\t\t6.统计学生总人数')
    print('\t\t\t\t\t\t7.显示所有学生信息')
    print('\t\t\t\t\t\t0.退出')
    print('------------------------------------------------------------------')

def insert():
    student_list=[]
    while True:
        id=input('请输入ID(如1001):')
        if not id:
            break
        name=input('请输入姓名:')
        if not name:
            break
        try:
            english=int(input('请输入英语成绩:'))
            python=int(input('请输入Python成绩:'))
            java=int(input('请输入Java成绩:'))
        except:
            print('输入无效,不是整数类型,请重新输入')
            continue
        #将录入的学生保存到字典中
        student={'id':id,'name':name,'english':english,'python':python,'java':java}
        #将学生信息添加到列表中
        student_list.append(student)
        answer=input('是否继续添加?y/n')
        if answer.lower()=='y':
            continue
        else:
            break

    #调用save()函数
    save(student_list)
    print('学生信息录入完毕!!!')

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()

def search():
    student_query=[]
    while True:
        id=''
        name=''
        if os.path.exists(filename):
            mode=input('按ID查找请输入1,按姓名查找请输入2:')
            if mode=='1':
                id=input('请输入学生ID:')
            elif mode=='2':
                name=input('请输入学生姓名:')
            else:
                print('您输入有误,请重新输入')
                continue
            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)
            #显示查询结果
            show_student(student_query)
            #清空列表
            student_query.clear()
            answer=input('是否要继续查询?y/n\n')
            if answer.lower()=='y':
                continue
            else:
                break
        else:
            print('暂未保存学生信息')
            return
def show_student(lst):
    if len(lst)==0:
        print('没有查询到学生信息,无数据显示!!!')
        return
    #定义标题显示格式
    format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}\t'
    print(format_title.format('ID','姓名','英语成绩','Python成绩','Java成绩','总成绩'))
    #定义内容显示格式
    format_data='{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}\t'
    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:
        student_id=input('请输入要删除的学生的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 #删除标记
            if student_old:
                with open(filename,'w',encoding='utf-8') as wfile:
                    d={}
                    for item in student_old:
                        d=dict(eval(item)) #将字符串转字典
                        if d['id']!=student_id:
                            wfile.write(str(d)+'\n')
                        else:
                            flag=True
                    if flag:
                        print(f'id为{student_id}的学生信息已被删除')
                    else:
                        print(f'没有找到ID为{student_id}的学生信息')
            else:
                print('无学生信息')
                break
            show() #删除之后要重新显示所有学生信息
            answer=input('是否继续删除?y/n')
            if answer.lower()=='y':
                continue
            else:
                break

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('请输入要修改的学员的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('找到学生信息,可以修改他的相关信息了!')
                while True:
                    try:
                        d['name']=input('请输入姓名:')
                        d['english']=input('请输入英语成绩:')
                        d['python']=input('请输入python成绩:')
                        d['java']=input('请输入java成绩:')
                    except:
                        print('您的输入有误,请重新输入!!!')
                    else:
                        break
                wfile.write(str(d)+'\n')
                print('修改成功!')
                show()
            else:
                print(f'未找到ID为{student_id}的学生信息')
                wfile.write(str(d)+'\n')
        answer=input('是否继续修改其他学生信息?y/n\n')
        if answer.lower()=='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 item in student_list:
            d=dict(eval(item))
            student_new.append(d)

    else:
        return
    asc_or_desc=input('请选择(0.升序,1.降序):')
    if asc_or_desc=='0':
        asc_or_desc_bool=False
    elif asc_or_desc=='1':
        asc_or_desc_bool=True
    else:
        print('您的输入有误,请重新输入')
        sort()
    mode=input('请选择排序方式(1.按英语成绩排序 2.按Python成绩排序 3.按Java成绩排序 4.按总成绩排序)')
    if mode=='1':
        student_new.sort(key=lambda x:int(x['english']),reverse=asc_or_desc_bool)
    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['python'])+int(x['java']),reverse=asc_or_desc_bool)
    else:
        print('您的输入有误,请重新输入')
        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'一共有{len(students)}名学生')
            else:
                print('还没有录入学生信息')
    else:
        print('暂未保存数据信息...')
def show():
    student_list = []
    if os.path.exists(filename):
        # 定义标题显示格式
        format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}\t'
        print(format_title.format('ID', '姓名', '英语成绩', 'Python成绩', 'Java成绩', '总成绩'))
        with open(filename,'r',encoding='utf-8') as rfile:
            student = rfile.readlines()
            for item in student:
                d = dict(eval(item))
                student_list.append(d)
        if len(student_list)==0:
            format_nodata = '{:^6}'
            print(format_nodata.format('无数据'))
        # 定义内容显示格式
        format_data = '{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}\t'
        for item in student_list:
            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'))
                                    ))
        student_list.clear()
    else:
        print('暂未保存过数据!!!')


if __name__ == '__main__':
    main()

标签:exe,python,else,item,student,input,print,信息管理系统,id
From: https://www.cnblogs.com/chenlei53/p/17278944.html

相关文章

  • Python | setattr函数的使用
    在Python中,setattr()是一个内置函数,用于设置对象的属性值,该属性不一定是存在的。语法setattr()的语法如下:setattr(obj,name,value)其中,obj是要设置属性值的对象,name是要设置的属性名,value是要设置的属性值。返回值为无。示例用法示例一:classPerson:def__in......
  • python opencv Sharpened
    pythonopencvSharpened importcv2importnumpyasnp#Loadtheimageimg=cv2.imread('20230222100736979.jpg')#Definethesharpeningkernelkernel=np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]])#Applythekerneltotheimagesharpened......
  • python对于虚拟环境的管理
    venv和conda的区别venv和conda都是用于创建虚拟环境和管理包的工具,但它们之间有一些不同。Python3.3以上,venv是内置的。它会直接在当前项目(当前文件夹下)安装Python虚拟环境,因此它是“去中心化”管理的,无法查看已安装的所有环境。conda是一个跨平台的开源包管理器,可以......
  • python-docx高级用法
    fromdocximportDocumentfromdocx.enum.sectionimportWD_SECTION#创建一个新文档doc=Document()#添加页眉header=doc.sections[0].headerheader.add_paragraph('Headertext')#添加页脚footer=doc.sections[0].footerfooter.add_paragraph('Footertext')#......
  • python数据挖掘与分析实战__电子商务网站用户行为分析及服务推荐
    importosimportpandasaspd#修改工作路径到指定文件夹os.chdir("D:/CourseAssignment/AI/CollectWebDate/")#第一种连接方式fromsqlalchemyimportcreate_engineengine=create_engine('mysql+pymysql://root:123456@localhost:3306/test?charset=utf8')sql......
  • python-pptx用法
    frompptximportPresentation#创建一个新PPT文档prs=Presentation()#添加幻灯片slide=prs.slides.add_slide(prs.slide_layouts[0])#保存PPT文档prs.save('example.pptx')frompptx.utilimportInches#添加文本框slide=prs.slides.add_slide(prs.slide_layouts[1])t......
  • python-docx常用方法
    fromdocximportDocument#创建一个新文档doc=Document()#添加段落para=doc.add_paragraph('HelloWorld!')fromdocx.enum.styleimportWD_STYLE_TYPEfromdocx.enum.textimportWD_ALIGN_PARAGRAPH#添加标题title=doc.add_heading('DocumentTitle',leve......
  • python多进程1:进程池使用时的死锁问题
    一、为什么?你正在使用多进程来在多个进程中运行一些代码,但它却“卡”住了。你查看CPU使用情况,却发现没有任何进展,无法完成任何工作。到底发生了什么?在许多情况下,你只需要加上一行代码就可以解决这个问题(可以跳到最后一句尝试一下),但是首先,让我们深入探讨Python的故障以及P......
  • chatGPT,该如何使用python调用你的接口
    以下是chatGPT的回答:好的,这里是一些使用Python调用OpenAIAPI的基本步骤:安装openaiPython包您可以使用pip安装openai包。在终端或命令行中输入以下命令:pipinstallopenai获取API密钥在使用OpenAIAPI之前,您需要获得OpenAIAPI密钥。要获得API密钥,请访问OpenAI网......
  • 【python】os模块
    os即operatingsystem,os模块提供的就是各种Python程序与操作系统进行交互的接口。读写文件建议使用内置函数withopen()。导入os模块importos列出路径——os.listdir()os.listdir(path='.')列出目录下的全部路径及文件。函数返回值是一个列表,其中元素为字符串......