import copy
students_list = []
student_information = {}
per_information = {}
def search_user(ID,password=False):
with open('users.txt', 'r', encoding='utf-8') as a:
user = '1'
while user:
user = a.readline()
user1 = user.split('|')
if user1[0] == ID:
if password:
if password == (user1[1][:-1]):
return True
else:
continue
else:
return True
else:
continue
return False
def main_page(ID):
"""显示主界面"""
print("===================================================================")
print("✿✿欢迎%s:\n" % ID)
print("\t❤ 1. 添加学生\n\t❤ 2. 查看学生\n\t❤ 3. 修改学生信息\n\t❤ 4. 删除学生\n\t❤ 5. 返回")
print("===================================================================")
# 数据关系 一个列表保存多个学生的个人信息,每个学生的个人信息保存学号,年龄,姓名等内容.
# 创建一个列表,列表里面包含许多个字典(个人信息),查找信息时,先遍历列表定位,
def creat_num():
count = 0
stu = 'stu'
while count <= 99999999:
count += 1
yield stu + str(count).rjust(8,'0')
def add_persnonal(number,index=None, edit=False):
"""添加个人信息并保存.如果edit = True,则在修改学生信息的时候使用
"""
if edit:
per_information.update({'学号': number})
name = input("请输入学生姓名:")
per_information.update({'姓名': name})
age = input("请输入学生年龄:")
per_information.update({'年龄': age})
tel = input("请输入学生电话:")
per_information.update({'电话': tel})
choice3 = input("确定修改?\n1.是\n2.否")
if choice3 == '1':
students_list[index] = copy.copy(per_information)
per_information.clear()
else:
per_information.update({'学号': number})
name = input("请输入学生姓名:")
per_information.update({'姓名': name})
age = input("请输入学生年龄:")
per_information.update({'年龄': age})
tel = input("请输入学生电话:")
per_information.update({'电话': tel})
students_list.append(copy.copy(per_information))
per_information.clear()
def all_info():
"""显示所有人信息"""
for index in range(len(students_list)):
for key in students_list[index]:
print(' ', key, students_list[index][key], end=' ')
print('\n')
def per_info(index,edit = False,):
"""
获取个人信息;如果edit = True,则在编辑学生信息的时候使用
"""
if edit == False:
for key in students_list[index]:
print(key, ' ', students_list[index][key], end=' ')
print('\n')
elif edit == True:
print(index+1, end="--- ")
for key in students_list[index]:
print(key, students_list[index][key], end=' ')
print("\n")
def search_(name,number,index_list,edit=False):
"""按名字/学号搜索,并生成搜索结果的索引的列表"""
if name:
for index in range(len(students_list)):
if students_list[index]['姓名'] != name:
continue
else:
index_list.append(index)
if len(index_list) == 0:
print("没有找到该学生!")
else:
if edit:
for index in index_list:
per_info(index, edit=True)
else:
for index in index_list:
per_info(index)
elif number:
for index in range(len(students_list)):
if students_list[index]['学号'] != number:
continue
else:
index_list.append(index)
if len(index_list) == 0:
print("没有找到该学生!")
else:
if edit:
for index in index_list:
per_info(index,edit=True)
else:
for index in index_list:
per_info(index)
def search_stu(name = False, number = False, edit =False , remove = False):
"""在为True的情况下分别对应:用学生姓名搜索/用学生学号搜索/修改学生信息的时候使用/删除学生信息的时候使用,edit和remove不能同时为True"""
index_list = []
if name:
if edit == False:
if remove == False:
search_(name=name,number=number,index_list=index_list)
else:
del_student(index_list,name)
else:
search_(name=name, number=number,index_list=index_list,edit=True)
index1 = int(input("请对应输入序号:"))
add_persnonal(index=index1-1,edit=True,number=number)
print("修改成功!")
if number:
if edit == False:
if remove == False:
search_(name=name, number=number, index_list=index_list)
else:
search_(name=name, number=number, index_list=index_list,edit=True)
for index in index_list:
per_info(index,edit=True)
index1 = int(input("请对应输入序号:"))
del students_list[index1-1]
else:
search_(name=name, number=number, index_list=index_list,edit=True)
index1 = int(input("请输入序号:"))
add_persnonal(index=index1-1,edit=True,number=number)
print("修改成功!")
def del_student(index_list, name=False,number=False):
"""用于删除学生信息,对应按学生姓名/学号查找并删除的情况"""
if name:
index_list = index_list
for index in range(len(students_list)):
if students_list[index]['姓名'] != name:
continue
else:
index_list.append(index)
for index in index_list:
per_info(index, edit=True)
index1 = int(input("请对应输入序号:"))
del students_list[index1 - 1]
elif number:
for index in range(len(students_list)):
if students_list[index]['学号'] != number:
continue
else:
index_list.append(index)
for index in index_list:
per_info(index, edit=True)
index1 = int(input("请对应输入序号:"))
del students_list[index1 - 1]
while True:
print("1.登录\n2.注册")
choice = input("请选择:")
if choice == '1':
ID = input("账户:")
password = input("密码:")
if len(password) < 2:
print("密码过短!")
continue
else:
if search_user(ID, password):
print("登录成功!")
else:
print("账户或密码有误!")
continue
elif choice == '2':
ID = input("新的账户:")
password = input("密码:")
if len(password) < 2:
print("密码过短!")
continue
else:
if search_user(ID):
print("该用户已经注册!")
continue
else:
users = ID + '|' + password+'\n'
with open('users.txt', 'a', encoding='utf-8') as a:
a.write(users)
print("注册成功!")
continue
else:
continue
num = creat_num()
while True:
# 主界面
main_page(ID)
choice1 = input("请选择(1-5):")
# 添加学生
if choice1 == '1':
while True:
number = next(num)
add_persnonal(number)
print("添加成功!\n1.继续\n2.返回")
value1 = input("请选择(1-2)")
if value1 == '1':
continue
else:
break
# 查看学生
elif choice1 == '2':
while True:
print("1.查看所有学生\n2.按姓名查找\n3.按学号查找\n4.返回")
choice2 = input("请选择(1-4)")
if choice2 == '1': # 1.查看所有学生
all_info()
elif choice2 == '2': # 2. 按姓名查找
name1 = input("请输入姓名:")
search_stu(name1)
elif choice2 == '3': # 3.按学号查找
number1 = input("请输入学号:")
search_stu(number=number1)
elif choice2 == '4':
break
else:
print("请重新输入!")
# 修改学生
# 修改学生信息
elif choice1 == '3':
# 先查找再修改(按名字,学号)
choice3 = input("输入查找方式:\n1.按姓名\n2.按学号\n返回")
if choice3 == "1":
name2 = input("请输入姓名:")
search_stu(name=name2,edit=True)
elif choice3 == '2':
number2 = input("请输入学号:")
search_stu(number=number2,edit=True)
# 删除学生
elif choice1== '4':
choice4 = input("输入查找方式:\n1.按姓名\n2.按学号\n3.返回")
if choice4 == '1':
name3 = input("请输入要删除学生的姓名:")
search_stu(name=name3, remove=True)
print("删除成功!")
elif choice4 == '2':
number3 = input("请输入要删除学生的学号:")
search_stu(name=number3, remove=True)
print("删除成功!")
# 返回
elif choice1 == '5':
break