编写一个简单的学生信息管理系统,需实现增、删、改、查的功能。
#!/usr/bin/python3
import os
def clear():
os.system("cls")
def pause():
os.system('pause')
class Student:
data = {"1": {"name": "晓王", "address": "玫瑰街5号"},
"2": {"name": "小秦", "address": "桃花路6号"},
"3": {"name": "小红", "address": "桃花路7号"},
"4": {"name": "小明", "address": "玫瑰街6号"},
"5": {"name": "小白", "address": "樱花街1号"},
"6": {"name": "小强", "address": "梅花路3号"}}
while 1:
pause()
clear()
print("1.增加数据\t2.删除数据\t3.修改数据\n4.查询数据\t5.退出程序")
mode = input("请输入模式: ")
if mode == "1":
clear()
id = input("请输入学生学号: ")
while id in data:
id = input("学号 " + id + " 已存在, 请重新输入: ")
name = input("请输入学生姓名: ")
address = input("请输入学生住址: ")
info = {id: {"name": name, "address": address}}
data.update(info)
print("添加成功")
if mode == "2":
clear()
id = input("请输入需要删除的学生学号\n输入 delall 删除全部: ")
if id == "delall":
data.clear()
print("已清空所有信息")
elif id in data:
del data[id]
print("已删除学号 " + id + " 的信息")
else:
print("学号" + id + "不存在")
if mode == "3":
clear()
id = input("请输入需要修改信息的学生的学号: ")
if id in data:
data[id]["name"] = input("请输入学号 " + id + " 的姓名: ")
data[id]["address"] = input("请输入学号 " + id + " 的住址: ")
else:
print("学号" + id + "不存在")
if mode == "4":
clear()
m = input("请输入需要查询的学生学号/姓名/住址\n输入 all 显示所有学生信息: ")
if m == "all":
if len(data) == 0:
print("未查询到任何记录")
else:
print("{0:10} {1:10} {2:10}".format("学号", "姓名", "住址"))
for key in data:
print("{0:10} {1:10} {2:10}".format(key, data[key]["name"], data[key]["address"]))
else:
print("{0:10} {1:10} {2:10}".format("学号", "姓名", "住址"))
sum = 0
for key in data:
if key.find(m) != -1:
sum += 1
print("{0:10} {1:10} {2:10}".format(key, data[key]["name"], data[key]["address"]))
elif data[key]["name"].find(m) != -1:
sum += 1
print("{0:10} {1:10} {2:10}".format(key, data[key]["name"], data[key]["address"]))
elif data[key]["address"].find(m) != -1:
sum += 1
print("{0:10} {1:10} {2:10}".format(key, data[key]["name"], data[key]["address"]))
if sum == 0:
clear()
print("未查询到任何记录")
if mode == "5":
break