import os
import json
file_name = "student.txt"
all_dict = {}
# 加载文件中的数据
def load_file():
# 如果没有文件,则创建文件
if not os.path.exists(file_name):
with open(file_name, "w", encoding="utf8") as f:
f.write("{}")
# 如果文件已经存在则读取文件
with open(file_name, "r", encoding="utf8") as f:
print(json.load(f))
# 添加
def add():
dic = {}
name = input("请您输入姓名: ").strip()
age = input("请您输入年龄: ").strip()
sex = input("请您输入性别: ").strip()
dic["name"] = name
dic["age"] = age
dic["sex"] = sex
if name in all_dict:
print("此学生已在系统内")
pass
else:
all_dict[name] = dic
# 删除
def dele():
name = input("请输入要删除的姓名: ").strip()
if name in all_dict:
del all_dict[name]
else:
print("系统中无此学生,删除毛线啊!")
# 修改
def update():
name = input("请输入要更新的学生姓名: ").strip()
if name in all_dict:
age = input("请输入年龄: ").strip()
sex = input("请输入性别: ").strip()
all_dict[name]["age"] = age
all_dict[name]["sex"] = sex
else:
print("系统中无此学生,更新个屁!")
# 查询
def select():
name = input("请输入要查询的学生姓名: ").strip()
if name in all_dict:
print(all_dict[name])
else:
print("无此学生,请添加!")
# 查询所有
def print_all():
if len(all_dict) > 0:
for k in all_dict:
print(all_dict[k])
else:
print("空的!")
def q():
exit()
# 函数字典
choice_dict = {
1: add,
2: dele,
3: update,
4: select,
5: print_all,
6: q
}
while True:
print("\033[31;1m欢迎登录博客园\n1:添加\n2:删除\n3:更新\n4:查询\n5:查看所有\n6:退出\033[0m")
choice = input("请输入要选择的编: ").strip()
if choice.isdigit():
choice = int(choice)
if choice > 0 and choice <= len(choice_dict):
choice_dict[choice]()
else:
print("您输入的编号超出范围, 看好后重新输入!")
else:
print("输入非法, 请重新输入!")