user_datas = []
student_datas = []
def read_user_from_file():
"""
从user.data文件中获取用户信息
"""
user_file = open("./user.data", "r", encoding="utf-8")
user_content = user_file.read()
global user_datas
user_datas = eval(user_content)
user_file.close()
def read_student_datas_from_file():
student_file = open("./student.data", "r", encoding="utf-8")
student_content = student_file.read()
global student_datas
student_datas = eval(student_content)
student_file.close()
def is_check_success(name, pwd):
flag = False
for user in user_datas:
if user["username"] == name and user["password"] == pwd:
return True
return flag
def show_operate_view():
print("-" * 30)
print("本学员管理系统可完成如下操作:")
print("\t\t1.添加学生;")
print("\t\t2.修改学生;")
print("\t\t3.删除学生;")
print("\t\t4.查询学生;")
print("\t\t5.保存信息;")
print("\t\t0.退出系统.")
print("-" * 30)
def add_student_info():
"""添加一个学生"""
print("添加学生的操作")
"""
学生列表中,append()
元素是字典,把一个字典添加到列表中
"""
name = input("")
gender = input("")
age = input("")
phone = input("")
student_dict = {'name': name, 'gender': gender, age: 'age', 'phone': phone}
student_datas.append(student_dict)
def update_student_info():
"""修改一个学生信息"""
print("请输入要修改的姓名")
name = input("")
gender = input("")
age = input("")
phone = input("")
for student in student_datas:
if name == student["name"]:
student["gender"] = gender
student["age"] = age
student["phone"] = phone
print("--------------------")
def delete_student_byname():
"""删除学生信息"""
print("-----------------删除学生的操作--------------")
name = input("请输入需要删除的学生的姓名")
index = 0
while index < len(student_datas):
delete = input("请确认是否删除y/n")
student_name = student_datas[index]["name"]
if student_name == name and (delete == "Y" or delete == "y"):
del student_datas[index]
print("已成功删除")
index += 1
def query_all_student():
"""查询学生"""
print("--------查询学生的操作-----------")
print("姓名\t性别\t年龄\t联系方式")
for student in student_datas:
name = student["name"]
gender = student["gender"]
age = student["age"]
phone = student["phone"]
print("%s\t%s\t\t%s\t\t%s\t" % (name, gender, age, phone))
def save_student_info():
"""保存学生信息"""
print("-----------------保存学生的操作------------------")
write_file = open("./student.data", "w", encoding="utf-8")
student_datas_str = str(student_datas)
write_file.write(student_datas_str)
write_file.close()
print("数据已经保存成功")
def main():
read_user_from_file()
read_student_datas_from_file()
print("-------------欢迎使用本学员管理系统-------------")
login_num = 0
global username
while login_num < 3:
username = input("请输入用户名:")
password = input("请输入登录密码:")
if is_check_success(username, password):
show_operate_view()
break
login_num += 1
if login_num == 3:
print("用户名和密码三次输入有误,登录失败")
else:
print("登录成功")
print("您好,%s" % username)
print("欢迎使用本学员管理系统")
while True:
number = int(input("请输入你要完后才能的操作序号(int):"))
if number == 1:
add_student_info()
elif number == 2:
update_student_info()
elif number == 3:
delete_student_byname()
elif number == 4:
query_all_student()
elif number == 5:
save_student_info()
elif number == 0:
print("你已经退出本系统")
break
else:
print("你输入的不存在")
if __name__ == '__main__':
main()