Python3 学生名片管理v2.0

v2.0是在1.0的基础上增加部分功能,实现将数据存入文件保存,以便于程序停止后还能再次取到数据。具体实现请看如下部分:

card_main.py

import sys
from One.card_func2 import *

def head():
    print('*' * 30)
    print('学生信息管理系统v2.0')
    print('1.添加学生\t2.显示所有\t3.查询学生\t0.退出系统')
    print('*' * 30)


def main():
    while True:
        head()
        num = input('输入操作:')
        if num == '1':
            print('选择功能添加学生')
            add_card()
        elif num == '2':
            print('选择功能添显示所有')
            show_all()
        elif num == '3':
            print('选择功能查询学生')
            search_card()
        elif num == '0':
            sys.exit()
        else:
            print('输入有误,命令无效')


if __name__ == '__main__':
    main()

card_func2.py

import json
import time

stutent_list = []

def add_card():
    name = input('请输入姓名:')
    age = input('请输入年龄:')
    qq = input('请输入QQ:')
    mail = input('请输入邮箱:')
    dict = {}
    dict['name'] = name
    dict['age'] = age
    dict['qq'] = qq
    dict['mail'] = mail
    stutent_list.append(dict)
    memory(stutent_list)

def memory(stutent_list): # 将数据存入文件
    with open('student.json', 'w') as f:
        for i in stutent_list:
            json_i = json.dumps(i)
            f.write(json_i+'\n')
    f.close()


def repick(): # 从文件中去除数据
    risk_result = []
    with open('student.json', 'r') as f:
        risk_new_list = f.read().split('\n')[:-1]
        for x in risk_new_list:
            json_x = json.loads(x)
            risk_result.append(json_x)
        f.close()
    return risk_result


def show_all():
    global stutent_list
    stutent_list = repick()
    table_head()
    for dict in stutent_list:
        print('-' * 32)
        print('| %s\t|\t%s\t|\t%s\t|\t%s\t|' % (dict['name'], dict['age'], dict['qq'], dict['mail']))



def search_card():
    global stutent_list
    name = input('请输入查找学生的姓名:')
    for dict in stutent_list:
        if dict['name'] == name:
            table_head()
            print('-' * 32)
            print('| %s\t|\t%s\t|\t%s\t|\t%s\t|' % (dict['name'], dict['age'], dict['qq'], dict['mail']))
            print('您可以执行操作:1.删除\t2.修改\t3.返回上一级')
            deal_card(dict)
            break
    else:
        print('查无此人!!!')


def table_head():
    print('_' * 32)
    print('| 姓名\t|\t年龄\t|\tQQ\t|\t邮箱\t|')


def deal_card(dict):
    order = input('请输入命令:')
    while True:
        if order == '1':
            delete_card(dict)
            break
        elif order == '2':
            modify_card(dict)
            break
        elif order == '3':
            break
        else:
            print('输入有误,命令无效')

def delete_card(dict):
    print('执行命令,删除数据')
    stutent_list.remove(dict)
    print('...正在删除,请等待...')
    turnTime()
    print('删除成功,返回主界面')


def modify_card(dict):
    print('执行命令,修改数据')
    dict['name'] = input('请输入姓名:')
    dict['age'] = input('请输入年龄:')
    dict['qq'] = input('请输入QQ:')
    dict['mail'] = input('请输入邮箱:')
    print('...正在修改,请等待...')
    turnTime()
    print('修改成功,返回主界面')


def turnTime():
    for i in list(range(5, 0, -1)):
        print(i, 's')
        time.sleep(1)

以下部分为控制台效果截图:

1.student.json中存储的数据


2.查询

Python3 学生名片管理v2.0_第1张图片

你可能感兴趣的:(Python3)