python习题之使用面向对象编写学生管理并持久化存储

使用面向对象编写 学生管理,持久化存储
输入数字1,添加学生信息(id,名字,年纪,性别)
第一个学生id为101 后续学生自动加1
输入数字2,查看所有学生信息
输入数字3,统计学生平均年纪
输入数字4,统计学生性别比例
输入数字0,退出系统
import pickle


class Student:
    def __init__(self, sid, name, age, sex):
        self.sid = sid
        self.name = name
        self.age = age
        self.sex = sex

    def __str__(self):
        return f"id:{self.sid},name:{self.name},age:{self.age},sex:{self.sex}"


class StudentManage:
    def __init__(self):
        self.students = []

    def save_data(self):
        with open("726.txt", 'wb') as f:
            pickle.dump(self.students, f)

    def load_data(self):
        try:
            with open("726.txt", 'rb') as f:
                self.students = pickle.load(f)
        except FileNotFoundError:
            self.students = []

    def add_student_name(self):
        while True:
            name = input("请输入姓名")
            if 2 <= len(name) <= 4:
                return name
            else:
                print(f"输入不合法")

    def add_student_age(self):
        while True:
            age = int(input("请输入年龄"))
            if 2 <= age <= 80:
                return age
            else:
                print(f"输入不合法")

    def add_student_sex(self):
        while True:
            sex = input("请输入性别")
            if sex in ["男", "女"]:
                return sex
            else:
                print(f"输入不合法")

    def continue_quit(self):
        option2 = input("继续(Y)退出(N)")
        if option2 == "Y" or option2 == "y":
            return option2
        if option2 == "n" or option2 == "N":
            return option2

    def add_student(self):
        while True:
            sid = 101 if not self.students else self.students[-1].sid + 1
            name = self.add_student_name()
            age = self.add_student_age()
            sex = self.add_student_sex()
            s = Student(sid, name, age, sex)
            self.students.append(s)
            print(f"添加成功,现在共有学生{len(self.students)}人")
            self.save_data()
            option2 = self.continue_quit()
            if option2 == "Y" or option2 == "y":
                pass
            else:
                break

    def show_all_student(self):
        if self.students:
            for s in self.students:
                print(s)
        else:
            print(f"还未添加学生")

    def get_avg_age(self):
        total = 0
        if self.students:
            for s in self.students:
                total += s.age
            print(f"平均年龄{total / len(self.students)}")
        else:
            print(f"还未添加学生")

    def get_sex_rate(self):
        man_count = 0
        women_count = 0
        if self.students:
            for s in self.students:
                if s.sex == "男":
                    man_count += 1
                else:
                    women_count += 1
            print(f"男生占比{man_count / len(self.students)},女生占比{women_count / len(self.students)}")
        else:
            print(f"还未添加学生")


class SystemManage(StudentManage):

    def main_menu(self):
        menu = """输入数字1,添加学生信息(id,名字,年纪,性别)
输入数字2,查看所有学生信息
输入数字3,统计学生平均年纪
输入数字4,统计学生性别比例
输入数字0,退出系统"""
        print(menu)

    def main_option(self):
        self.load_data()
        while True:
            self.main_menu()
            option = input("请输入选项")
            if option not in [str(i) for i in range(0, 5)]:
                print(f"输入不合法")
            elif option == "1":
                self.add_student()
            elif option == "2":
                self.show_all_student()
            elif option == "3":
                self.get_avg_age()
            elif option == "4":
                self.get_sex_rate()
            elif option == "0":
                self.save_data()
                print(f"退出系统成功")
                break


s = SystemManage()
s.main_option()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值