Table of Contents
1. System functions
2. System development environment
3. Implementation code
4. Package the project and generate the .exe executable file
Home Backend Development Python Tutorial How to implement a simple student information management system in python

How to implement a simple student information management system in python

May 12, 2023 am 09:43 AM
python

1. System functions

1. Enter student information

2. Search student information

3. Modify student information

4. Delete students Information

5. Grade sorting

6. Statistics of the total number of students

7. Display all student information

0. Exit the system

2. System development environment

1. Operating system: win7

2. Development tools: PyCharm

3. Python built-in modules: os,re

3. Implementation code

import os
filename='students.txt'
def main():
    while True:
        menum()
        choice=int(input('请选择功能:'))
        if choice in [0,1,2,3,4,5,6,7]:
            if choice==0:
                answer=input('您确定要退出系统吗?(y/n)')
                if answer=='y' or answer=='Y':
                    print('感谢您的使用!')
                    break #退出while循环并退出系统
                else:
                    continue
            elif choice == 1:
                insert() #录入学生信息
            elif choice == 2:
                search() #查找学生信息
            elif choice == 3:
                delete() #删除学生信息
            elif choice == 4:
                modify() #修改学生信息
            elif choice == 5:
                sort() #成绩排序
            elif choice == 6:
                total() #统计学生总人数
            elif choice == 7:
                show() #显示所有学生信息
 
def insert():
    student_list=[] #用于存储学生信息,列表每个元素是字典。
    while True:
        id=input('请输入学生ID:')
        if not id:
            break
        name=input('请输入学生姓名:')
        if not name:
            break
        try:
            Englist=int(input('请输入英语成绩:'))
            Python=int(input('请输入Python成绩:'))
            Java=int(input('请输入Java成绩:'))
        except:
            print('输入无效,不是整数类型,请重新输入!')
            continue
        #将录入的学生信息保存到字典中
        student={'id':id,'name':name,'English':Englist,'Python':Python,'Java':Java}
        #将学生信息添加到列表中
        student_list.append(student)
        answer=input('是否继续添加?(y/n)')
        if answer=='y' or answer=='Y':
            continue
        else:
            break
 
    #将学生信息保存在文件中
    save(student_list)
    print('学生信息录入完毕!')
    pass
 
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('您的输入有误,请重新输入!')
                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)
            #显示查询结果
            show_student(student_query)
            student_query.clear() #清空列表
            answer=input('是否要继续查询?(y/n)')
            if answer=='y' or answer=='Y':
                continue
            else:
                break
        else:
            print('暂未保存学生信息。')
            return
    pass
 
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=='y' or answer=='Y':
                continue
            else:
                break
    pass
 
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('请输入English成绩:')
                        d['Python']=input('请输入Python成绩:')
                        d['Java']=input('请输入Java成绩:')
                    except:
                        print('您的输入有误请重新输入!')
                    else:
                        break
                wfile.write(str(d)+'\n')
                print('修改成功!')
            else:
                wfile.write(str(d)+'\n')
        answer=input('是否继续修改其他学生信息?(y/n)')
        if answer=='y':
            modify()
    pass
 
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('请选择排序方式(0:按总成绩排序,1:按English成绩排序,2:按Python成绩排序,3:按Java成绩排序)')
    if mode=='0':
        student_new.sort(key=lambda x: int(x['English'])+int(x['Python'])+int(x['Java']), reverse=asc_or_desc_bool)
    elif 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)
    else:
        print('您的输入有误,请重新输入!')
        sort()
    show_student(student_new)
    pass
 
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('暂未保存学生信息......')
    pass
 
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('暂未保存过数据!')
    pass
 
def save(stu_list):
    try:
        stu_txt=open(filename,'a',encoding='utf=8')
    except:
        stu_txt=open(filename,'w',encoding='utf-8')
    for item in stu_list:
        stu_txt.write(str(item)+'\n')
    stu_txt.close()
 
def show_student(lst):
    if len(lst)==0:
        print('没有查到学生信息,无数据显示!')
        return
    #定义标题显示格式
    format_title='{:^6}\t{:^12}\t{:^10}\t{:^10}\t{:^10}\t{:^8}'
    print(format_title.format('ID','姓名','English成绩','Python成绩','Java成绩','总成绩'))
    #定义内容显示格式
    format_data='{:^6}\t{:^12}\t{:^10}\t{:^18}\t{:^14}\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'),str(int(item.get('English'))+int(item.get('Python'))+int(item.get('Java')))))
    pass
 
def menum():
    print('-------------------学生信息管理系统--------------------')
    print('-----------------------功能菜单------------------------')
    print('                    1.录入学生信息')
    print('                    2.查找学生信息')
    print('                    3.删除学生信息')
    print('                    4.修改学生信息')
    print('                    5.成绩排序')
    print('                    6.统计学生总人数')
    print('                    7.显示所有学生信息')
    print('                    0.退出系统')
    print('------------------------------------------------------')
 
main()
Copy after login

4. Package the project and generate the .exe executable file

1. Install the third-party module, open the dos window, enter: pip install PyInstaller and press Enter,

2. Enter: pyinstaller -F the specific location of the program file,

3. After pressing Enter, you can see the location of the .exe file in the penultimate line of the output content.

The above is the detailed content of How to implement a simple student information management system in python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

See all articles