吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5087|回复: 21
收起左侧

[Python 转载] Python 学生信息管理系统(搬运,觉得很好分享一下)

[复制链接]
融汇木子 发表于 2022-2-25 16:57
一个学生信息管理系统。功能挺多的,界面如下:
155922txzlxx4devvk4dcf.png
[Python] 纯文本查看 复制代码
import pickle
from os.path import isfile
from os import remove


def main():
    running = True
    while running:
        menu()
        code = '9'
        count = 0
        while code not in [str(i) for i in range(8)]:
            if count == 0:
                code = input("请输入:")
            else:
                code = input("输入有误!请重新输入:")
            count += 1
        if code == '0':
            running = False
        elif code == '1':
            insert()
        elif code == '2':
            search()
        elif code == '3':
            delete()
        elif code == '4':
            modify()
        elif code == '5':
            sort()
        elif code == '6':
            total()
        elif code == '7':
            show()
    else:
        input("感谢您使用学生信息查询系统程序,请按 <Enter> 键退出程序!")


def menu():
    print('''
    ╔———————学生信息管理系统————————╗
    │                                              │
    │   =============== 功能菜单 ===============   │
    │                                              │
    │   1 录入学生信息                             │
    │   2 查找学生信息                             │
    │   3 删除学生信息                             │
    │   4 修改学生信息                             │
    │   5 排序                                     │
    │   6 统计学生总人数                           │
    │   7 显示所有学生信息                         │
    │   0 退出系统                                 │
    │  ==========================================  │
    │  说明:通过数字选择菜单                      │
    ╚———————————————————————╝
        ''')


def insert():
    continue_ = 'y'
    while continue_ == 'y':
        ID = input("请输入 ID(如 1001):")
        name = input("请输入名字:")
        chinese = input("请输入语文成绩:")
        while not is_float(chinese):
            chinese = input("输入无效!请重新输入语文成绩:")
        chinese = float(chinese)
        math = input("请输入数学成绩:")
        while not is_float(math):
            math = input("输入无效!请重新输入数学成绩:")
        math = float(math)
        english = input("请输入英语成绩:")
        while not is_float(english):
            english = input("输入无效!请重新输入英语成绩:")
        english = float(english)

        score = [{'id': ID, 'name': name,
                  'chinese': chinese, 'math': math, 'english': english}]

        if not isfile("students.pkl"):
            file = open("students.pkl", "ab")
            pickle.dump(score, file)
            file.close()
        else:
            file = open("students.pkl", "rb+")
            score_list = pickle.load(file)
            score_list.extend(score)
            file.close()
            file = open("students.pkl", "wb")
            pickle.dump(score_list, file)
            file.close()

        continue_ = input("是否继续添加?(y/n)")
        while continue_ not in ['y', 'n']:
            continue_ = input("输入无效!是否继续添加?(y/n)")

    else:
        print("学生信息录入完毕!")


def search():
    continue_ = 'y'
    while continue_ == 'y':
        if not isfile("students.pkl"):
            print("没有任何数据!")
            return
        way = input("按ID查输入1;按姓名查输入2:")
        while way not in ['1', '2']:
            way = input("输入无效!按ID查输入1;按姓名查输入2:")
        if way == '1':
            students = pickle.load(open("students.pkl", "rb"))
            ID = input("请输入学生 ID:")
            result = []
            for each in students:
                if each['id'] == ID:
                    result.append(each)
        else:
            students = pickle.load(open("students.pkl", "rb"))
            name = input("请输入学生姓名:")
            result = []
            for each in students:
                if each['name'] == name:
                    result.append(each)
        result_str = ''
        result_str += 'ID'.center(6)
        result_str += '姓名'.center(12)
        result_str += '语文'.center(10)
        result_str += '数学'.center(10)
        result_str += '英语'.center(10)
        result_str += '总成绩'.center(11)
        for i in result:
            result_str += "\n"
            result_str += i['id'].center(6)
            result_str += i['name'].center(12)
            result_str += str(i['chinese']).center(12)
            result_str += str(i['math']).center(12)
            result_str += str(i['english']).center(12)
            result_str += str(i['chinese'] + i['math'] +
                              i['english']).center(13)
        print(result_str)
        continue_ = input("是否继续查询 (y/n)?")
        while continue_ not in ['y', 'n']:
            continue_ = input("输入无效!是否继续查询 (y/n)?")


def delete():
    if not isfile("students.pkl"):
        print("没有学生信息!@_@")
        return
    continue_ = 'y'
    while continue_ == 'y':
        show()
        ID = input("请输入要删除的学生ID:")
        students = pickle.load(open("students.pkl", "rb"))
        result = []
        for each in students:
            if each['id'] == ID:
                result.append(each)
        if not result:
            print(f"没有找到ID为 {ID} 的学生信息...")
            show()
        else:
            for i in result:
                students.remove(i)
            if not students:
                remove("students.pkl")
                print(f"ID 为 {ID} 的学生已成功删除!不可继续删除!")
                return
            else:
                pickle.dump(students, open("students.pkl", "wb"))
                print(f"ID 为 {ID} 的学生已成功删除!")
        continue_ = input("是否继续删除 (y/n)?")
        while continue_ not in ['y', 'n']:
            continue_ = input("输入无效!是否继续删除 (y/n)?")


def modify():
    if not isfile("students.pkl"):
        print("没有学生信息!")
        return
    show()
    continue_ = 'y'
    while continue_ == 'y':
        ID = input("请输入要修改的学生ID:")
        students = pickle.load(open("students.pkl", "rb"))
        result = []
        for each in students:
            if each['id'] == ID:
                result.append(each)
        if not result:
            print(f"没有找到ID为 {ID} 的学生信息...")
            show()
        elif len(result) != 1:
            print("ID 有相同的情况!")
        else:
            students.remove(result[0])
            name = input("请输入名字:")
            chinese = input("请输入语文成绩:")
            while not is_float(chinese):
                chinese = input("输入无效!请重新输入语文成绩:")
            chinese = float(chinese)
            math = input("请输入数学成绩:")
            while not is_float(math):
                math = input("输入无效!请重新输入数学成绩:")
            math = float(math)
            english = input("请输入英语成绩:")
            while not is_float(english):
                english = input("输入无效!请重新输入英语成绩:")
            english = float(english)

            score = {'id': ID, 'name': name,
                     'chinese': chinese, 'math': math, 'english': english}
            students.append(score)
            pickle.dump(students, open("students.pkl", "wb"))
        continue_ = input("是否继续修改 (y/n)?")
        while continue_ not in ['y', 'n']:
            continue_ = input("输入无效!是否继续修改 (y/n)?")


def sort():
    if not isfile("students.pkl"):
        print("没有学生信息!")
        return
    show()
    reverse = input("请选择(0升序;1降序):")
    while reverse not in ['0', '1']:
        reverse = input("输入无效!请选择(0升序;1降序):")
    reverse = bool(int(reverse))
    students = pickle.load(open("students.pkl", "rb"))
    way = input("请选择排序方式"
                "(1按语文成绩排序;"
                "2按数学成绩排序;"
                "3按英语成绩排序;"
                "0按总成绩排序):")
    while way not in ['0', '1', '2', '3']:
        way = input("输入无效!请选择排序方式"
                    "(1按语文成绩排序;"
                    "2按数学成绩排序;"
                    "3按英语成绩排序;"
                    "0按总成绩排序):")
    if way == '0':
        def condition(x):
            return x['chinese'] + x['math'] + x['english']
    elif way == '1':
        def condition(x):
            return x['chinese']
    elif way == '2':
        def condition(x):
            return x['math']
    else:
        def condition(x):
            return x['english']
    result = sorted(students, key=condition, reverse=reverse)
    result_str = ''
    result_str += 'ID'.center(6)
    result_str += '姓名'.center(12)
    result_str += '语文'.center(10)
    result_str += '数学'.center(10)
    result_str += '英语'.center(10)
    result_str += '总成绩'.center(11)
    for i in result:
        result_str += "\n"
        result_str += i['id'].center(6)
        result_str += i['name'].center(12)
        result_str += str(i['chinese']).center(12)
        result_str += str(i['math']).center(12)
        result_str += str(i['english']).center(12)
        result_str += str(i['chinese'] + i['math'] +
                          i['english']).center(13)
    print(result_str)


def total():
    try:
        print("一共有",
              str(len(pickle.load(open("students.pkl", "rb")))), "名学生!")
    except FileNotFoundError:
        print("没有学生信息!")


def show():
    if not isfile("students.pkl"):
        print("没有学生信息!@_@")
        return
    result = pickle.load(open("students.pkl", "rb"))
    result_str = ''
    result_str += 'ID'.center(6)
    result_str += '姓名'.center(12)
    result_str += '语文'.center(10)
    result_str += '数学'.center(10)
    result_str += '英语'.center(10)
    result_str += '总成绩'.center(11)
    for i in result:
        result_str += "\n"
        result_str += i['id'].center(6)
        result_str += i['name'].center(12)
        result_str += str(i['chinese']).center(12)
        result_str += str(i['math']).center(12)
        result_str += str(i['english']).center(12)
        result_str += str(i['chinese'] + i['math'] +
                          i['english']).center(13)
    print(result_str)


def is_float(number):
    try:
        float(number)
    except ValueError:
        return False
    else:
        return True


if __name__ == '__main__':
    try:
        main()
    except BaseException as e:
        print("啊哦,出错了 O_O")
        print(e)
        input("请按 <Enter> 键退出程序!")

免费评分

参与人数 10吾爱币 +4 热心值 +10 收起 理由
Dark_Forest + 1 再搞个tkinter上去,妥妥的!
52shine + 1 + 1 谢谢@Thanks!
猫吃鱼呀 + 1 用心讨论,共获提升!
出入平安 + 1 谢谢@Thanks!
cogi + 1 + 1 谢谢@Thanks!
FJLKXL + 1 谢谢@Thanks!
xiaojiuwoer008 + 1 + 1 我很赞同!
DreamStars + 1 我很赞同!
wh2510 + 1 谢谢@Thanks!
wanfon + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

恋爱选举巧克力 发表于 2022-2-25 18:55
你早发电我的python程序设计就是它了哈哈哈哈
zjc3024 发表于 2022-2-25 18:58
ToT鱼 发表于 2022-2-25 20:12
zz1181 发表于 2022-2-25 21:14
谢谢分享,学习一下
DreamStars 发表于 2022-2-25 21:34
好像在小破站看到过类似的,正好学习一下
zljiscx 发表于 2022-2-25 22:29
看到这个界面想起了当初学批处理。
小白2021 发表于 2022-2-25 22:38
可以修改成其他的管理系统,这个的确很实用。感谢分享,代码清晰明了。
anzhongcha 发表于 2022-2-25 22:46
py经验值得学习参考。
lili95 发表于 2022-2-26 17:23
学习学习,谢谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 07:48

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表