python實現學生管理系統源碼

本文實例為大傢分享瞭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('........回車鍵繼續操作........')

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: