python学生管理系统

该代码实现了一个基于Python的简单学生管理系统,使用列表和字典存储学生信息,具备添加、删除、修改、查询和显示所有学生信息的功能。但由于未使用文件,数据无法保存,每次运行都需要重新输入数据。
摘要由CSDN通过智能技术生成

学生管理系统(Python,无文件版)

  1. 由于这段代码是用列表和字典存储的,没有用到文件,无法保存数据,
   所以每次需要重新输入数据
  2.本系统有添加学生、删除学生、修改学生信息、查询学生信息、显示所有学生信息功能

存储列表

Infomation =[]

系统界面函数

#系统界面函数
def System():
    print('-' * 30)
    print("Welcome to the Student Management System")
    print("Please select the feature you want to operate:")
    print('-' * 30)
    #1.添加学生信息 2。删除学生信息 3.修改学生信息 4.查询学生信息 5.显示全部学生信息 6.退出系统
    print("1.Add student information")
    print("2.Delete student information")
    print("3.Modify student information ")
    print("4.Query student information")
    print("5.Show all student information")
    print("6.Exit the system")
    print('-' * 30)

添加学生函数

#添加学生信息函数
def add():
    #输入添加人数
    num = int(input("Please enter the number of added:"))
    while(num):
        #声明全局变量Infomation
        global Infomation
        #输入学生信息
        sname = input("Enter the student's name:")
        sno = input("Enter the student number:")
        age = input("Enter the student's age:")
        sex = input("Enter the gender of the student:")
        telephone = input("Enter the student's telephone:")
        cls = input("Enter the student class:")
        #检查学生信息是否已经存在,若存在退出循环
        for i in Infomation:
            if sno == i['sno']:
                print("The student already exists!")
                return
        #学生信息不存在,将学生信息追加到列表中
        Infomation_dict = {}
        Infomation_dict['sname'] = sname
        Infomation_dict['sno'] = sno
        Infomation_dict['age'] = age
        Infomation_dict['sex'] = sex
        Infomation_dict['telephone'] = telephone
        Infomation_dict['cls'] = cls
        Infomation.append(Infomation_dict)
        print(Infomation)
        print("\n")
        num-=1

删除学生信息(会删除所有与输入内容相同的学生)

#删除内容编号
def Delete_num():
    print('-' * 30)
    print("1.Delete by sname")
    print("2.Delete by sno")
    print("3.Delete by age")
    print("4.Delete by sex")
    print("5.Delete by telephone")
    print("6.Delete by class")
    print("7.exit")
    print('-' * 30)
#删除学生信息函数
def delete():
    while True:
        global Infomation
        Delete_num()
        num = int(input("Please select the feature you want to operate:"))
        #按名字删除(删除所有与输入的名称相同的学生)
        if num == 1:
            sname = input("Please enter the name you want to delete:")
            #判断学员是否存在
            for i in Infomation:
                if sname == i['sname']:
                    Infomation.remove(i)
                    print("Delete succeeded!")

            else:
                print("The student does not exist!")
            n = int(input("Continue to delete? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                print(Infomation)
                return
        #按学号删除
        elif num == 2:
            sno = input("Please enter the number you want to delete:")
            # 判断学员是否存在
            for i in Infomation:
                if sno == i['sno']:
                    Infomation.remove(i)
                    print("Delete succeeded!")

            else:
                print("The student does not exist!")
            n = int(input("Continue to delete? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                print(Infomation)
                return
        #按年龄删除
        elif num == 3:
            age = input("Please enter the age you want to delete:")
            # 判断学员是否存在
            for i in Infomation:
                if age == i['age']:
                    Infomation.remove(i)
                    print("Delete succeeded!")

            else:
                print("The student does not exist!")
            n = int(input("Continue to delete? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                print(Infomation)
                return
        #按性别删除
        elif num == 4:
            sex = input("Please enter the sex of student you want to delete:")
            # 判断学员是否存在
            for i in Infomation:
                if sex == i['sex']:
                    Infomation.remove(i)
                    print("Delete succeeded!")

            else:
                print("The student does not exist!")
            n = int(input("Continue to delete? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                print(Infomation)
                return
        #按电话号码删除
        elif num == 5:
            telephone = input("Please enter the telephone of student you want to delete:")
            # 判断学员是否存在
            for i in Infomation:
                if telephone == i['telephone']:
                    Infomation.remove(i)
                    print("Delete succeeded!")

            else:
                print("The student does not exist!")
            n = int(input("Continue to delete? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                print(Infomation)
                return
        #按班级删除
        elif num == 6:
            cls = input("Please enter the class of student you want to delete:")
            # 判断学员是否存在
            for i in Infomation:
                if cls == i['cls']:
                    Infomation.remove(i)
                    print("Delete succeeded!")

            else:
                print("The student does not exist!")
            n = int(input("Continue to delete? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                print(Infomation)
                return
        elif num == 7:
            return
        else:
            print("Input error,please re-enter!")

修改学生信息

#修改内容序号
def Modify_num():
    print('-' * 30)
    print("1.Modify sname")
    print("2.Modify sno")
    print("3.Modify age")
    print("4.Modify sex")
    print("5.Modify telephone")
    print("6.Modify class")
    print("7.exit")
    print('-' * 30)
#修改学生信息
def modify():
    #输入要修改学生的学号
    sno = input("Please enter the student number to be modified:")
    global Infomation
    #判断学生是否存在
    for i in Infomation:
        if sno == i['sno']:
            while True:
                Modify_num()
                num = int(input("Please select the feature you want to operate:"))
                #修改姓名
                if num == 1:
                    i['sname'] = input("Please enter a new name:")
                    print("Modity succeeed!")
                    print(Infomation)
                    n = int(input("Continue to modify? Press any number to continue,Press 0 to exit:"))
                    if(n == 0):
                        print(Infomation)
                        return
                #修改学号
                elif num == 2:
                    new_sno = input("Please enter a new student number:")
                    #保证学号的唯一性
                    for j in Infomation:
                        if new_sno == j['sno']:
                            print("Student number already exists,Please reselect")
                            break
                    else:
                        j['sno'] = new_sno
                        print("Modity succeeed!")
                        print(Infomation)
                    n = int(input("Continue to modify? Press any number to continue,Press 0 to exit:"))
                        if(n == 0):
                            print(Infomation)
                              return
                #修改年龄
                elif num == 3:
                    i['age'] = input("Please enter new age:")
                    print("Modity succeeed!")
                    print(Infomation)
                    n = int(input("Continue to modify? Press any number to continue,Press 0 to exit:"))
                    if (n == 0):
                        print(Infomation)
                        return
                #修改性别
                elif num == 4:
                    i['sex'] = input("Please enter new sex:")
                    print("Modity succeeed!")
                    print(Infomation)
                    n = int(input("Continue to modify? Press any number to continue,Press 0 to exit:"))
                    if (n == 0):
                        print(Infomation)
                        return
                #修改电话号码
                elif num == 5:
                    i['telephone'] = input("Please enter a new telephone:")
                    print("Modity succeeed!")
                    print(Infomation)
                    n = int(input("Continue to modify? Press any number to continue,Press 0 to exit:"))
                    if (n == 0):
                        print(Infomation)
                        return
                #修改班级
                elif num == 6:
                    i['cls'] = input("Please enter a new class:")
                    print("Modity succeeed!")
                    print(Infomation)
                    n = int(input("Continue to modify? Press any number to continue,Press 0 to exit:"))
                    if (n == 0):
                        print(Infomation)
                        return
                elif num == 7:
                    return
                else:
                    print("Input error,please re-enter!")
    else:
        print("The student does not exist!")

查询学生信息

#查询内容序号
def Query_num():
    print('-' * 30)
    print("1.Query by name")
    print("2.Query by sno")
    print("3.Query by age")
    print("4.Query by sex")
    print("5.Query by telephone")
    print("6.Query by class")
    print("7.exit")
    print('-' * 30)
#查询学生信息函数
def query():
    while True:
        Query_num()
        num = int(input("Please enter the query method serial number:"))
        #按姓名查询
        if num == 1:
            name = input("Please enter the name of the student:")
            for i in Infomation:
                if name == i['sname']:
                    print("The student's information is as follows:")
                    print(f"The student's name is {i['sname']},sno is {i['sno']},age is {i['age']},sex is {i['sex']},telephone is {i['telephone']},class is {i['cls']}")
            else:
                print("The student does not exist!")
            n = int(input("Continue to query? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
               return
        #按学号查询
        elif num == 2:
            sno = input("Please enter the number of the student:")
            for i in Infomation:
               if sno == i['sno']:
                    print("The student's information is as follows:")
                    print(f"The student's name is {i['sname']},sno is {i['sno']},age is {i['age']},sex is {i['sex']},telephone is {i['telephone']},class is {i['cls']}")
            else:
               print("The student does not exist!")
            n = int(input("Continue to query? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                return
        #按年龄查询
        elif num == 3:
            age = input("Please enter the age of the student:")
            for i in Infomation:
               if age == i['age']:
                    print("The student's information is as follows:")
                    print(f"The student's name is {i['sname']},sno is {i['sno']},age is {i['age']},sex is {i['sex']},telephone is {i['telephone']},class is {i['cls']}")
            else:
               print("The student does not exist!")
            n = int(input("Continue to query? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                return
        #按性别查询
        elif num == 4:
            sex = input("Please enter the sex of the student:")
            for i in Infomation:
               if sex == i['sex']:
                    print("The student's information is as follows:")
                    print(f"The student's name is {i['sname']},sno is {i['sno']},age is {i['age']},sex is {i['sex']},telephone is {i['telephone']},class is {i['cls']}")
            else:
               print("The student does not exist!")
            n = int(input("Continue to query? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                return
        #按电话号码查询
        elif num == 5:
            telephone = input("Please enter the telephone of the student:")
            for i in Infomation:
               if telephone == i['telephone']:
                    print("The student's information is as follows:")
                    print(f"The student's name is {i['sname']},sno is {i['sno']},age is {i['age']},sex is {i['sex']},telephone is {i['telephone']},class is {i['cls']}")
            else:
               print("The student does not exist!")
            n = int(input("Continue to query? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                return
        #按班级查询
        elif num == 6:
            cls = input("Please enter the class of the student:")
            for i in Infomation:
               if cls == i['cls']:
                    print("The student's information is as follows:")
                    print(f"The student's name is {i['sname']},sno is {i['sno']},age is {i['age']},sex is {i['sex']},telephone is {i['telephone']},class is {i['cls']}")
            else:
               print("The student does not exist!")
            n = int(input("Continue to query? Press any number to continue,Press 0 to exit:"))
            if (n == 0):
                return
        elif num == 7:
            return
        else:
            print("Input error,please re-enter!")

显示所有学生信息

#显示所有学生信息
def all():
    print("All student information is as follows:")
    for i in Infomation:
        print( f"The student's name is {i['sname']},sno is {i['sno']},age is {i['age']},sex is {i['sex']},telephone is {i['telephone']},class is {i['cls']}")

主程序

while True:
    System()
    num = int(input("Input the num of operate:"))
    if   num == 1:
        add()
    elif num == 2:
        delete()
    elif num == 3:
        modify()
    elif num == 4:
        query()
    elif num == 5:
        all()
    elif num == 6:
        exit = int(input("Are you sure you want to exit? 1.yes 2.no:"))
        if exit == 1:
            break
    else:
        print("Input error,please re-enter!")

界面展示

主界面

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值