Python實現學生管理系統


1.代碼

from prettytable import PrettyTable

class StudentInfo:
    def __init__(self):
        print("---------------------------")
        print(" 學生管理系統 V1.0 ")
        print(" 1:添加學生 ")
        print(" 2:顯示所有學生 ")
        print(" 3:查詢學生 ")
        print(" 4:修改學生 ")
        print(" 5:刪除學生 ")
        print(" 6:退出系統 ")
        print("---------------------------")
        self.student_list = [] # 定義一個列表用來存儲所有的學生信息(每個學生是一個字典)
        self.table = PrettyTable()
        self.table.field_names = ["序號", "姓名", "性別", "年齡"]

    def AddStudent(self):
        """添加學生信息"""
        student = {}  # 定義一個字典,用來存儲用戶的學生信息
        name = input("請輸入學生姓名>>>")
        # 判斷學生是否存在
        for i in self.student_list:
            if i['name'] == name:
                print("學生已經存在!!")
                return None
        sex = input("請輸入學生性別>>>")
        age = input("請輸入學生年齡>>>")
        # 向字典中添加數據
        student['name'] = name
        student['sex'] = sex
        student['age'] = age
        # 向列表中添加這個字典
        self.student_list.append(student)
        # 向表格里添加一行
        self.table.add_row([self.student_list.index(student)+1, name, sex, age])
        print("添加成功!!!")

    def UpdateTable(self):
        '''更新表格'''
        self.table.clear_rows()
        for i, student in enumerate(self.student_list):
            self.table.add_row([i + 1, student['name'], student['sex'], student['age']])
        print(self.table)

    def PrintTable(self):
        """打印表格"""
        print(self.table)

    def SearchStudent(self):
        """查詢學生信息"""
        index = int(input("請輸入要查找學生的序號:"))
        if 1 <= index <= len(self.student_list):
            print(self.table[index-1])  # 打印該學生信息
        else:
            print("輸入的學生序號有誤,請重新輸入")

    def UpdateStudent(self):
        '''修改學生信息'''
        index = int(input("請輸入要修改學生的序號:"))
        if 1 <= index <= len(self.student_list):
            print(self.table[index-1])  # 打印該學生信息
        else:
            print("輸入的學生序號有誤,請重新輸入")
        name = input("請輸入學生姓名>>>")
        # 判斷學生是否存在
        for student in self.student_list:
            if student['name'] == name:
                print("學生已經存在!!")
                return None
        self.student_list[index-1]['name'] = name
        self.student_list[index-1]['sex'] = input("請輸入學生性別>>>")
        self.student_list[index-1]['age'] = input("請輸入學生年齡>>>")
        print("修改成功!")
        print(self.student_list)
        self.UpdateTable()
        return None

    def DeleteStudent(self):
        """刪除學生信息"""
        print(self.table)
        index = int(input("請輸入要刪除學生的序號:"))
        if 1 <= index <= len(self.student_list):
            del_flag = input("你確定要刪除么?[y/n]")
            if del_flag == 'y':
                self.student_list.pop(index-1)
                print("刪除成功!!!")
                self.UpdateTable()
                return None
            else:
                print("放棄刪除!!!")
                return None
        else:
            print("輸入序號有誤,請重新輸入")

    def main(self):
        while True:
            choose = int(input("請輸入的您的選擇>>>"))
            if choose == 1:
                self.AddStudent()
            elif choose == 2:
                self.PrintTable()
            elif choose == 3:
                self.SearchStudent()
            elif choose == 4:
                self.UpdateStudent()
            elif choose == 5:
                self.DeleteStudent()
            elif choose == 6:
                print("Bye~~")
                break
            else:
                print("輸入有誤,請重新輸入......")


if __name__ == '__main__':
    stu = StudentInfo()
    stu.main()

 

2.運行效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM