python实现学生管理系统源码

编辑: admin 分类: python 发布时间: 2021-12-24 来源:互联网

本文实例为大家分享了python实现学生管理系统的具体代码,供大家参考,具体内容如下

一.面向过程版

import os

stu_list = []


def show_menu():
    print('1.添加学生')
    print('2.删除学生')
    print('3.修改学生信息')
    print('4.查询单个学生信息')
    print('5.查询所有学生信息')
    print('6.退出系统')


def insert_student():
    name = input('请输入学生名字:')
    for stu in stu_list:
        if stu['name'] == name:
            print('.........学生信息已存在........')
            return
    age = input('请输入学生年龄:')
    gender = input('请输入学生性别:')
    stu_dict = {'name': name, 'age': int(age), 'gender': gender}
    stu_list.append(stu_dict)
    print("学生信息添加成功!")


def remove_student():
    name = input('请输入要操作的学生的名字:')
    for stu in stu_list:
        if stu['name'] == name:
            stu_list.remove(stu)
            print('删除成功!')
            break
    else:
        print('........该学生信息不存在,无法删除.........')


def modify_student():
    name = input('请输入要操作的学生的名字:')
    for stu in stu_list:
        if stu['name'] == name:
            stu['age'] = int(input('请输入新的年龄:'))
            print('修改成功!')
            break
    else:
        print('........该学生信息不存在,无法修改.........')


def search_student():
    name = input('请输入要操作的学生的名字:')
    for stu in stu_list:
        if stu['name'] == name:
            print(f'姓名:{stu["name"]},年龄:{stu["age"]},性别:{stu["gender"]}')
            break
    else:
        print('........该学生信息不存在.......')


def show_all_stu():
    if len(stu_list):
        for stu in stu_list:
            print(f'姓名:{stu["name"]},年龄:{stu["age"]},性别:{stu["gender"]}')
    else:
        print("目前没有学生信息!")


def save():
    f = open('student.txt', 'w')
    f.write(str(stu_list))
    f.close()


def read_file():
    global stu_list
    if os.path.exists('student.tct'):
        f = open('student.txt', 'r', encoding='utf-8')
        buf = f.read()
        if buf:
            stu_list = eval(buf)
        f.close()


def main():
    read_file()
    while True:
        show_menu()
        opt = input('请输入用来选择的编号:')
        if opt == '1':
            print('1.添加学生')
            insert_student()
        elif opt == '2':
            print('2.删除学生')
            remove_student()
        elif opt == '3':
            print('3.修改单个学生信息')
            modify_student()
        elif opt == '4':
            print('查询单个学生信息')
            search_student()
        elif opt == '5':
            print('5.查询所有学生信息')
            show_all_stu()
        elif opt == '6':
            print('欢迎下次使用本系统')
            save()
            break
        else:
            print('输入有误,请重新输入')
            continue
        input('........回车键继续操作........')


main()

二.面向对象版

1.工程文件

2.main.py

import student_manage_sysytem as sms

if __name__ == '__main__':
    stu_sms = sms.StudenManagerSystem()
    stu_sms.start()

3.student.py

class Student():
    def __init__(self, stu_id, name, age, gender):
        self.sut_id = stu_id
        self.name = name
        self.age = age
        self.gender = gender

    def __str__(self):
        return f"{self.sut_id},{self.name},{self.age},{self.gender}"

4.student_manage_system.py

import student


class StudenManagerSystem():
    def __init__(self):
        self.stu_dict = {}

    @staticmethod
    def __show_menu():
        print('1.添加学生')
        print('2.删除学生')
        print('3.修改学生信息')
        print('4.查询单个学生信息')
        print('5.查询所有学生信息')
        print('6.退出系统')

    def __insert_student(self):
        stu_id = input('请输入学生学号:')
        if stu_id in self.stu_dict:
            print('学生信息已经存在,不需要重复添加')
            return
        name = input('请输入学生名字:')
        age = input('请输入学生年龄:')
        gender = input('请输入学生性别:')
        stu = student.Student(stu_id, name, age, gender)
        self.stu_dict[stu_id] = stu

    def __removw_student(self):
        stu_id = input('请输入学号:')
        if stu_id in self.stu_dict:
            del self.stu_dict[stu_id]
            print('学生已经删除')
        else:
            print('学生信息不存在,无法删除')

    def __modify_student(self):
        stu_id = input('请输入学号:')
        if stu_id in self.stu_dict:
            stu = self.stu_dict[stu_id]
            stu.age = input('请输入新的年龄:')
            print('信息已经修改完毕')
        else:
            print('学生信息不存在,无法修改')

    def __search_student(self):
        stu_id = input('请输入学号:')
        if stu_id in self.stu_dict:
            stu = self.stu_dict[stu_id]
            print(stu)
        else:
            print('学生信息不存在')

    def __save(self):
        f = open('student.txt', 'w', encoding='utf-8')
        for stu in self.stu_dict.values():
            f.write(str(stu) + '\n')
        f.close()

    def __load_info(self):
        try:
            f = open('student.txt', 'r', encoding='utf-8')
            buf_list = f.readlines()
            for buf in buf_list:
                buf = buf.strip()
                info_list = buf.split(',')
                stu = student.Student(*info_list)
                stu_id = info_list[0]
                self.stu_dict[stu_id] = stu
            f.close()
        except Exception:
            pass

    def __show_all_info(self):
        for stu in self.stu_dict.values():
            print(stu)

    def start(self):
        self.__load_info()
        while True:
            self.__show_menu()
            opt = input('请输入用来选择的编号:')
            if opt == '1':
                print('1.添加学生')
                self.__insert_student()
            elif opt == '2':
                print('2.删除学生')
                self.__removw_student()
            elif opt == '3':
                print('3.修改单个学生信息')
                self.__modify_student()
            elif opt == '4':
                print('查询单个学生信息')
                self.__search_student()
            elif opt == '5':
                print('5.查询所有学生信息')
                self.__show_all_info()
            elif opt == '6':
                self.__save()
                print('欢迎下次使用本系统')
                break
            else:
                print('输入有误,请重新输入')
                continue
            input('........回车键继续操作........')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持hwidc。

【文章原创作者http://www.yidunidc.com/aqt.html 网络转载请说明出处】