猫史档案馆


【Python作品分享】学生信息管理系统【作品秀】

用户:金鳞丶混知金鳞丶混知查看:0 回复:3 评论:0 创建时间:2021-06-23T19:35:56


【作品展示】

center_image

 

【作品介绍】

这是一个学生系统管理系统,采用Python语言编写,同时采用JSON文件格式来储存数据,理论上可以无限制的储存学生的信息,目前可以储存学生的姓名、年龄及ID,还可以通过后期完善储存更多的信息,且此程序易重构代码

 

【作品源代码】

import os
import json

size = os.path.getsize('students.json')
if size == 0:
    students = []
    with open('students.json', 'w') as f:
        json.dump(students, f)


def idSet():
    '''对ID进行设置'''
    with open('students.json') as f:
        students = json.load(f)
    i = 0
    c = 0

    if students:
        while c <= len(students):
            b = 0
            for student in students:
                if c != student['ID']:
                    b = b + 1
            if b == len(students):
                return c
            elif b == 0:
                return students[-1]['ID'] + 1
            c += 1
    else:
        return 0


def add():
    '''添加学生'''
    with open('students.json') as f:
        students = json.load(f)
    name = ""
    age = ""
    name = input("请输入姓名:\n")
    age = input("请输入年龄:\n")
    int(age)
    students.append({'name': name, 'age': age, 'ID': idSet()})
    with open('students.json', 'w') as f:
        json.dump(students, f)


def delete():
    '''删除学生'''
    with open('students.json') as f:
        students = json.load(f)
    d_ID = input("请输入要删除学生的ID:\n")
    i = 0
    while i < len(students):
        if int(d_ID) == students[i]['ID']:
            del students[i]
            break
        i += 1
    with open('students.json', 'w') as f:
        json.dump(students, f)


def change():
    '''修改学生的信息'''
    with open('students.json') as f:
        students = json.load(f)
    d_ID = input("请输入要修改信息学生的ID:\n")
    int(d_ID)
    i = 0
    while i < len(students):
        if int(d_ID) == students[i]['ID']:
            del students[i]
            print("重新输入学生信息")
            add()
            break
        i += 1
    with open('students.json', 'w') as f:
        json.dump(students, f)


def printer():
    '''显示所有学生'''
    with open('students.json') as f:
        students = json.load(f)

    for student in students:
        print(
            f"\n姓名:{student['name']}\t年龄:{student['age']}\tID:{student['ID']}\n")


a = ""
while True:
    print("---------------学生信息管理系统---------------")
    print("1.增加学生\n2.删除学生\n3.修改学生信息\n4.所有学生\n(输入q退出))")
    a = input('请输入选项:\n')
    if a == '1':
        add()
    elif a == '2':
        delete()
    elif a == '3':
        change()
    elif a == '4':
        printer()
    elif a == 'q':
        print("\n谢谢您的使用!")
        break

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
lsk666666lsk666666

emotion_编程猫_点赞

点赞0


评论


Python-happyyyyPython-happyyyy

赞哦emotion_编程猫_点赞

点赞0


评论


天之鹰天之鹰

emotion_编程猫_厉害了

点赞0


评论