Arliki

博客园 首页 新随笔 联系 订阅 管理

学生管理系统   更新学生信息没做出来,找个大神补全下。谢谢。

# 记录:
# bug:操作后若退出需要两次退出才行。 待修复
# 下一步:链接redis进行使用。
# 更新学生库信息   待完成

import re
import redis,chardet
class stu_card:
    cont = {}
    def main(self, student):
        print("")
        print("1 : 增加学生信息卡;2 : 删除学生信息卡;\n3 : 查看学生信息卡;4 : 修改学生信息卡;\n5 : 查看所有学生信息卡;6 : 退出学生信息系统;\n7:更新学生库信息")
        num = input("请选择所需功能:")
        if num == "1":
            self.add_card()
            return False
        elif num == "2":
            self.del_card()
            return False
        elif num == "3":
            self.show_card()
            return False
        elif num == "4":
            self.edit_card()
            return False
        elif num == "5":
            self.all_card()
            self.main(student)
            return False
        elif num == "6":
            print("您已退出该系统")
            return False
        elif num == "7":
            self.stu_upd()
            return False
        elif num == "":
            self.main(student)
            return False
        else:
            print("操作错误,自动退出")
            return False

    # 添加学生  引导并获取值
    def add_card(self):
        self.cont = {}
        name = input("请输入姓名:")
        self.check_input(1, name)
        sax = input("请输入性别: (男/女)")
        self.check_input(2, sax)
        age = input("请输入年龄: (数字)")
        self.check_input(3, age)
        stu_nu = input("请输入学号:(数字)")
        self.check_input(4, stu_nu)
        self.info_card()

    # 删除学生
    def del_card(self):
        if len(student) == 0:
            print("学生卡为空,请及时添加!")
        else:
            for nu, stu in enumerate(student):
                print(nu + 1, "号学生:", "姓名:", stu['name'], "性别:", stu['sax'], "年龄:", stu['age'], "学号:", stu['stu_nu'])
            del_nu = input("请选择要删除的学生编号:")
            del_nu = int(del_nu) - 1
            if del_nu <= len(student):
                r.hdel('users',del_nu)
                student.pop(del_nu)
                print("删除成功!")
            else:
                print("信息有误,操作失败")
        self.main(student)

    # 搜索学生  引导并获取值
    def show_card(self):
        print("精确搜索:\n     1 姓名;2 学号;\n范围搜索:\n     3 性别;4 年龄段(以空格隔开); ")
        search = int(input("请选择搜索条件:"))
        if search == 1:
            name = input("请输入姓名:")
            self.check_input(1, name)
            self.search('name')
        elif search == 2:
            stu_nu = input("请输入学号:")
            self.check_input(4, stu_nu)
            self.search('stu_nu')
        elif search == 3:
            sax = input("请输入性别:")
            self.check_input(2, sax)
            self.search('sax')
        elif search == 4:
            age = input("请输入年龄段:")
            age = self.check_input(5, age)
            self.search('age', age)
        else:
            return False

    # 编辑学生信息  引导并获取值
    def edit_card(self, ):
        self.all_card()
        edit = input("请输入学生编号进行修改:")
        self.all_card(nu=int(edit))

    # 提交学生修改、添加
    def info_card(self, num=-1):
        if num == -1:
            student.append(self.cont)
            r.hset('users',r.hlen('users'),str(self.cont))
            print("学生:", self.cont['name'], "信息添加成功")
            self.cont = {}
            self.main(student)
        elif num >= 0:
            student[num] = self.cont
            r.hset('users', num, str(self.cont))
            print("学生:", self.cont['name'], "信息更新成功")
            self.cont = {}
            self.main(student)
        elif num >= len(student):
            print("信息有误,操作失败")
            self.main(student)
        return False

    # 处理显示信息
    def all_card(self, stu=[], nu=-1):
        if nu == -1:
            if stu == []:
                if len(student) == 0:
                    print("当前没有学生信息,请及时添加!")
                else:
                    for nu, stu in enumerate(student):
                        print(nu + 1, "号学生:", "姓名:", stu['name'], "性别:", stu['sax'], "年龄:", stu['age'], "学号:",
                              stu['stu_nu'])
            elif stu == ['no search']:
                print("搜索结果为空")
                self.main(student)
            else:
                print("查询共有", len(stu), "位学生")
                for nu, st in enumerate(stu):
                    print(nu + 1, "号学生:", "姓名:", st['name'], "性别:", st['sax'], "年龄:", st['age'], "学号:",
                          st['stu_nu'])
        elif nu >= 1 and nu <= len(student):
            self.cont = {}
            nu = nu - 1
            print("您正在修改", student[nu]['name'], "的信息,不修改请直接回车")
            self.cont['name'] = student[nu]['name']
            self.cont['sax'] = student[nu]['sax']
            self.cont['age'] = student[nu]['age']
            self.cont['stu_nu'] = student[nu]['stu_nu']
            new_name = input("原姓名:%s;新姓名:" % (student[nu]['name']))
            if len(new_name.strip(' ')) > 0:
                del self.cont['name']
                self.check_input(1, new_name)
            new_sax = input("原性别:%s;新性别:" % (student[nu]['sax']))
            if len(new_sax.strip(' ')) > 0:
                del self.cont['sax']
                self.check_input(2, new_sax)
            new_age = input("原年龄:%s;新年龄:" % (student[nu]['age']))
            if len(new_age.strip(' ')) > 0:
                del self.cont['age']
                self.check_input(3, new_age)
            new_stu_nu = input("原学号:%s;新学号:" % (student[nu]['stu_nu']))
            if len(new_stu_nu.strip(' ')) > 0:
                del self.cont['stu_nu']
                self.check_input(4, new_stu_nu)
            self.info_card(nu)
        else:
            print("编号有误,请从新选择")
            self.edit_card()

    # 检查输入信息
    def check_input(self, nu, che):
        if nu == 1:
            if 'name' in self.cont.keys():
                pass
            else:
                che = re.sub('[0-9]', ' ', che)
                che = che.strip(' ')
                if che == '':
                    print("姓名输入错误")
                    name = input("请输入姓名:")
                    self.check_input(1, name)
                else:
                    self.cont['name'] = che
                    return True
        elif nu == 2:
            if 'sax' in self.cont.keys():
                pass
            else:
                if che == "" or che == "":
                    self.cont['sax'] = che
                    return True
                else:
                    print("性别格式错误")
                    sax = input("请输入性别: (男/女)")
                    self.check_input(2, sax)
        elif nu == 3:
            if 'age' in self.cont.keys():
                pass
            else:
                if che.isdigit() and int(che) >= 6 and int(che) <= 70:
                    self.cont['age'] = che
                    return True
                else:
                    print("学生年龄不合法!")
                    age = input("请输入年龄: (数字)")
                    self.check_input(3, age)
        elif nu == 4:
            if 'stu_nu' in self.cont.keys():
                pass
            else:
                if che.isdigit():
                    self.cont['stu_nu'] = int(che)
                    return True
                else:
                    print("学号不正确,请重新输入")
                    stu_nu = input("请输入学号:(数字)")
                    self.check_input(4, stu_nu)
        elif nu == 5:
            che = che.split(' ')
            che[0] = int(che[0])
            che[1] = int(che[1])
            if che[0] < che[1]:
                return che
            else:
                print("年龄不正确,请重新输入")
                age = input("请输入年龄段(以空格隔开):")
                self.check_input(5, age)
        else:
            return False

    # 处理搜索学生信息
    def search(self, nu, che=[]):
        stu = []
        if nu == 'name' or nu == 'sax' or nu == 'stu_nu':
            for st in student:
                if self.cont[nu] in st[nu]:
                    stu.append(st)
            if stu == []:
                stu = ['no search']
        elif nu == 'age':
            for st in student:
                if che[0] <= st['age'] and che[1] >= st['age']:
                    stu.append(st)
            if stu == []:
                stu = ['no search']
        self.all_card(stu)
    #更新学生库     待完成
    def stu_upd(self):
        print("我也想更新啊")
        self.main(student)
stu = stu_card()
pool=redis.ConnectionPool(host='localhost',port=6379,password="5tgbnhy67ujm")
r=redis.Redis(connection_pool=pool)
print("欢迎进入学生管理系统!")
print("功能详情:")
student=r.hvals('users')      #获取学生信息
n_student=[]
for v in student:
    #user=chardet.detect(v)         #检测编码类型
    user=v.decode('utf-8')
    n_student.append(eval(user))  #str转dict并重组student
student=n_student
stu.main(student)
# while stu.main(student) != False:
#     pass
问题源码

更加希望能够优化下。

redis存储学生信息用的是hash,key是自然数,val是字符串(json格式的),编码是utf-8

 

邮箱: hkdnxycz@outlook.com  谢谢

posted on 2018-01-03 15:44  小程序源码下载  阅读(237)  评论(0编辑  收藏  举报