【Python习题】学生信息管理系统(简易版)

题目内容

创建一个包含以下功能的学生信息的管理系统

  1. 添加学生信息
  2. 删除学生信息
  3. 修改学生信息
  4. 查询所有学生信息
  5. 退出系统

个人解法

# 创建信息空表
info = []
 
# 打印系统菜单
def menu():
    print('=' * 30)
    print('1.添加学生信息')
    print('2.查看学生信息')
    print('3.删除学生信息')
    print('4.修改学生信息')
    print('5.查询学生信息')
    print('0.退出系统')
    print('=' * 30)
 
# 打印单个学生信息
def print_info_single(x, blank):
    def unpack_dict(ordinal, name, gen, tel): # 可以顺带练习一下字典解包求value值(双星号法),虽然会有点绕
        return ordinal, name, gen, tel
    val1, val2, val3, val4 = unpack_dict(**x)
    if len(val4) > 2:
        blank = blank - len(val4)//2
    print(f"{val1}\t" + " " * blank + f"{val2}\t" + " " * blank + f"{val3}\t" + " " * blank + f"{val4}\t")
 
# 添加学生信息
def add_info():
    global info
    new_name = input("请输入姓名:")
    new_gen = input("请输入性别:")
    new_tel = input("请输入手机号:")
    #new_id = input("请输入学号:")
    stu = {'ordinal': str(len([ele for ele in info if isinstance(ele, dict)]) + 1), 'name': new_name, 'gen': new_gen, 'tel': new_tel} #stu = {'ordinal': str(len([ele for ele in info if isinstance(ele, dict)]) + 1), 'name': new_name, 'gen': new_gen, 'tel': new_tel, 'id': new_id}
    try:
        for x in info:
            if new_name == x['name']:
                print("请重新输入!")
                return
    except:
        print('表格出现异常!')
        return
    else:
        try:
            if len(info) != 0:
                info.insert(int(input('请输入要添加到的位置序号:\n')), stu)
            else:
                info.append(stu)
        except:
            print("输入有误!")
            return
        else:
            print("信息添加成功!")
 
# 查询学生信息
def check_info():
    if len(info) != 0:
        print('=' * 30)
        blank = int(input("请设置文字间距:"))
        print("序号\t" + " " * blank + "姓名\t" + " " * blank + "性别\t" + " " * blank + "手机号")
        for x in info:
            print_info_single(x, blank)
    else:
        print("信息表为空!")
 
# 删除学生信息
def del_info():
    global info
    if len(info) != 0:
        try:
            for stu in info:
                if input("请输入要删除的学生序号:") == stu['ordinal']:
                    info.remove(stu)
                    break
        except:
            print('该学生不存在!')
        else:
            print("信息添加成功!")
    else:
        print("信息表为空!")
 
# 修改学生信息
def modify_info():
    global info
    if len(info) != 0:
        try:
            for stu in info:
                if input("请输入要修改的学生序号:") == stu['ordinal']:
                    stu['name'] = input("请输入新的姓名:")
                    stu['gen'] = input("请输入新的性别:")
                    stu['tel'] = input("请输入新的手机号:")
                    #stu['id'] = input("请输入新的学号:")
                    break
        except:
            print("该学生不存在!")
        else:
            print("信息添加成功!")
    else:
        print("信息表为空!")

#查询学生信息
def search_info():
    if len(info) != 0:
        try:
            for stu in info:
                if input("请输入要查询的学生序号:") == stu['ordinal']:
                    blank = int(input("请设置文字间距:"))
                    print("该学生的信息如下:\n" + "序号\t" + " " * blank + "姓名\t" + " " * blank + "性别\t" + " " * blank + "手机号")
                    print_info_single(stu, blank)
                    break
        except:
            print("该学生不存在!")
        else:
            print("信息添加成功!")
    else:
        print("信息表为空!")

#主函数
def main():
    while True:
        menu()
        user_num = eval(input('请输入功能对应的数字:'))
        if user_num == 1:
            add_info()
        elif user_num == 2:
            check_info()
        elif user_num == 3:
            del_info()
        elif user_num == 4:
            modify_info()
        elif user_num == 5:
            search_info()
        elif user_num == 0:
            exit_flag = input("确认退出?(请输入Y/N)?\n")
            if exit_flag == 'Y':
                break
        else:
            print('请重新输入!')
 
if __name__ == '__main__':
    main()

运行结果

【Python习题】学生信息管理系统(简易版)_第1张图片

本人拙作,请大佬们点评。 

你可能感兴趣的:(Python习题,python)