# 学员管理系统
# # 需求:1.添加学员 2.修改学员 3.查询学员 4.删除学员 0.退出系统
# # 注意:
# # 将数据存入到本地文件中, 数据是永久存储的 ,下一次运行程序要进行读取数据
# # 业务需求根据自己的理解去实现
# # 培养业务能力
# # 展现数据时,界面友好
import pickle
from prettytable import PrettyTable
students = [
{'name': 'test1', 'age': 19, 'sex': '男', 'phone': '123456'},
{'name': 'test2', 'age': 19, 'sex': '女', 'phone': '123456'},
{'name': 'test1', 'age': 29, 'sex': '女', 'phone': '654321'}
]
def save_data():
# 已二进制形式将数据保存到文件
with open('data.pkl', 'wb') as f:
pickle.dump(students, f)
print('保存成功')
# save_data()
def load_data():
try:
# 已二进制形式读取文件
with open('data.pkl', 'rb') as f:
data = pickle.load(f)
if data: # 如果列表不为空
global students
students = data
except IOError:
pass
def add_student():
print("************添加学员***********")
name = input("请输入学员姓名:")
age = input("请输入学员年龄:")
sex = input("请输入学员性别(男/女):")
phone = input("请输入学员手机号:")
student = dict(name=name, age=age, sex=sex, phone=phone)
# students列表是一个可变对象
students.append(student)
print("添加成功")
def query_student():
print("************查询学员***********")
print("1.查询全部学员")
print("2.输入姓名查询学员")
select = int(input("请选择:"))
while select not in range(1, 3):
select = int(input("输入的选项有误,请重新输入"))
if select == 1:
if students:
for i in range(0, len(students)):
# student = students[i]
# name = student["name"]
# age = student["age"]
# sex = student["sex"]
# phone = student["phone"]
student_list = list(students[i].values())
student_list.insert(0, i + 1)
pt.add_row(student_list)
elif select == 2:
search_name = input("请输入学生姓名")
for i in range(0, len(students)):
student = students[i]
if student["name"] == search_name:
student_list = list(student.values())
student_list.insert(0, i + 1)
pt.add_row(student_list)
break
print(pt)
def modify_student():
# 修改学员
search_name = input("请输入学员姓名")
index = -1
for i in range(0, len(students)):
student = students[i]
if student["name"] == search_name:
index = i
break
if index != -1:
student_list = list(students[index].values())
student_list.insert(0, 1)
pt.add_row(student_list)
print(pt, end='\n\n')
new_name = input("请输入新的学生姓名")
new_age = int(input("请输入新的年龄"))
new_sex = input("请输入新的年龄")
new_phone = input("请输入新的电话")
student = dict(name=new_name, age=new_age, sex=new_sex, phone=new_phone)
students[index] = student
print("修改成功")
else:
print("没有对应的学生信息", end='\n\n')
# 加载数据
load_data()
pt = PrettyTable(field_names=["序号", "姓名", "年龄", "性别", "电话"])
pt.align["序号"] = "l" # 已序号左对齐
pt.padding_width = 1 # 填充宽度
while 1: # 死循环
print("***************学员管理系统***************")
print("*************** 1.添加学员 ***************")
print("*************** 2.修改学员 ***************")
print("*************** 3.查询学员 ***************")
print("*************** 4.删除学员 ***************")
print("*************** 0.退出系统 ***************")
num = int(input("请选择你的操作:"))
pt.clear_rows()
# 判断用户输入的数,如果不在0-4的范围,则提示错误
while num not in range(0, 5):
num = int(input("您输入的选项有误,请重新输入:"))
if num == 1:
add_student()
save_data()
elif num == 2:
modify_student()
elif num == 3:
query_student()
elif num == 4:
print('删除学员')
elif num == 0:
print('谢谢使用,再见!')
break
|