当前位置:
首页
文章
前端
详情

Python 学生管理

python3 练手 

#coding=utf-8
#学生类
class Stu:
    #三个字段id字符串,name字符串,score小数
    def __init__(self, id, name, score):
        self.id = id
        self.name = name
        self.score = score
    #将类作为字符串输出
    def __str__(self):
        return self.id + '\t' + self.name + '\t' + str(self.score)

#管理学生的类
class StuMgr:
    #由给定路径初始化stus列表
    def __init__(self, path):
        self.path = path
        self.stus = []
        with open(self.path, 'r+', encoding='utf-8') as f:
            n = int(f.readline())
            for i in range(n):
                s = f.readline().split()  # 默认空格分割
                stu = Stu(str(s[0]), str(s[1]), float(s[2]))
                self.stus.append(stu)
    #由给定的id查找具体学生,不存在返回None
    def findById(self, id):
        for s in self.stus:
            if s.id == id:
                return s
        return None
    #由给定的id删除具体学生
    def delById(self, id):
        if self.findById(id) != None:
            for s in range(len(self.stus)):
                if self.stus[s].id == id:
                    self.stus.pop(s)
            return True
        else:
            return False

    def add(self, stu):
        if self.findById(stu.id) != None:
            return False
        else:
            self.stus.append(stu)
            return True
    #按照id排序
    def sortById(self):
        self.stus.sort(key=lambda Stu: Stu.id)

    #保存到文件,编码utf--8码
    def saveToFile(self):
        with open(self.path, 'w+', encoding='utf-8') as f:
            f.write(str(len(self.stus)))
            f.write('\n')
            for s in self.stus:
                f.write(str(s) + '\n')

    def show(self):
        print('共 %d 名学生' % (len(self.stus)))
        for s in self.stus:
            print(s)
    #用于绘制界面,提供操作
    def start(self):
        while True:
            try:
                print("1,显示\t2,添加\t3,删除\t4,查找\t5,排序\t6,保存\t -by ahao")
                opt = input();
                if opt == '1':
                    self.show()
                elif opt == '2':
                    print('请输入要添加的学生信息,空格隔开\n学号\t\t姓名\t成绩')
                    ss = input().split(' ')
                    stu = Stu(str(ss[0]), str(ss[1]), float(ss[2]))
                    self.add(stu);
                elif opt == '3':
                    print('请输入需要删除的学生id')
                    id = input()
                    if self.findById(id) != None:
                        self.delById(id)
                        print('删除成功')
                    else:
                        print('删除失败,该学号不存在')
                elif opt == '4':
                    print('请输入查找的学生学号')
                    id = input()
                    if self.findById(id) != None:
                        s = self.findById(id)
                        print(s)
                    else:
                        print('该学生不存在')
                elif opt == '5':
                    self.sortById()
                    print('排序成功')
                elif opt == '6':
                    self.saveToFile()
                    print('保存成功')
                else:
                    print('请输入有效命令')
            except BaseException:
                print('非法数据,请检查输入是否合法')

#初始化文件路径
stuMgr = StuMgr('stu.txt')
stuMgr.start();

stu. 

6
2150500094    陈文梁    85.79
2160500079    崔致琪    83.44
2160500080    侯玥林    86.53
2160500081    刘佳雯    92.33
2160500082    石可心    86.72
2160500111    yyh    123.0

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。