import os.path as os_path
filename = "students.txt"
def main():
while True:
menus()
choose_menu = int(input("请选择功能:"))
if (choose_menu in [0, 1, 2, 3, 4, 5, 6, 7]):
if (choose_menu == 0):
confirm = input('您确认要退出系统吗?y/n\n')
if (confirm == "y"):
break
else:
continue
else:
if (choose_menu == 1):
insert()
elif (choose_menu == 2):
search()
elif (choose_menu == 3):
delete()
elif (choose_menu == 4):
update()
elif (choose_menu == 5):
sort()
elif (choose_menu == 6):
total()
elif choose_menu == 7:
show()
else:
continue
def menus():
print("============================学生信息管理系统==================================")
print("--------------------------------系统菜单-------------------------------------")
print(''' 1.录入学生信息 ''')
print(''' 2.查找学生信息 ''')
print(''' 3.删除学生信息 ''')
print(''' 4.修改学生信息 ''')
print(''' 5.排序 ''')
print(''' 6.统计学生人数 ''')
print(''' 7.显示所有学生信息 ''')
print(''' 0.退出系统 ''')
def insert():
stu_list = []
while True:
name = input("请输入学生姓名:")
if not name:
break
id = input("请输入学号,例如1001:")
if not id:
break
try:
php = int(input("请输入php成绩:"))
java = int(input("请输入java成绩:"))
net = int(input("请输入.net成绩:"))
student = {'name': name, 'id': id,
'php': php, 'java': java, 'net': net}
stu_list.append(student)
is_continue = input("继续添加吗?y/n\n")
if is_continue == 'y':
continue
else:
break
except:
continue
save(stu_list)
def save(list):
try:
if (os_path.exists(filename)):
f_name = open(filename, "a+", encoding="utf-8")
else:
f_name = open(filename, "w", encoding="utf-8")
for item in list:
f_name.write(str(item)+"\n")
f_name.close()
except Exception as e:
print(e)
def delete():
if (os_path.exists(filename)):
while True:
show()
id = input("请输入学生id:")
if id != '':
with open(filename, "r", encoding="utf-8") as file:
stu_list = file.readlines()
if stu_list:
flag = False
with open(filename, "w", encoding="utf-8") as w_file:
for item in stu_list:
stu = dict(eval(item))
if (int(stu['id']) != int(id)):
w_file.write(item)
else:
print("学生已删除成功")
flag = True
if (flag):
is_continue = input("继续删除学生信息吗?y/n")
if is_continue == 'y':
show()
continue
else:
break
else:
print("未找到该学生信息")
continue
else:
print("没有学生信息")
break
else:
print("输入信息不合法")
continue
else:
print("未录入学生信息")
main()
def update():
if (os_path.exists(filename)):
while True:
show()
id = input("请输入学生id:")
if id != '':
with open(filename, "r", encoding="utf-8") as r_file:
stu_list = r_file.readlines()
if (stu_list):
flag = False
with open(filename, 'w', encoding="utf-8") as w_file:
for item in stu_list:
stu = dict(eval(item))
if (int(stu['id']) == int(id)):
flag = True
name = input("请输入学生姓名:")
php = int(input("请输入php成绩:"))
java = int(input("请输入java成绩:"))
net = int(input("请输入net成绩:"))
stu_new = {'name': name, 'id': int(
id), 'php': php, 'java': java, 'net': net}
w_file.write(str(stu_new)+"\n")
else:
w_file.write(str(stu)+"\n")
if (flag):
print("修改完成\n")
is_continue = input("是否继续修改?y/n")
if is_continue == "y":
continue
else:
break
else:
print("学生信息不存在")
continue
else:
print("学生信息为空")
break
else:
print("输入格式有误")
continue
else:
print("未录入学生信息")
main()
def search():
if (os_path.exists(filename)):
while True:
show()
type = input("请选择查找模式:1按id查询,2按名字查询:")
id = ''
name = ''
if (int(type) == 1):
id = input("请输入学生id:")
elif (int(type) == 2):
name = input("请输入学生名字:")
else:
continue
with open(filename, "r", encoding="utf-8") as r_file:
stu_list = r_file.readlines()
stu_search_list = []
for item in stu_list:
stu = dict(eval(item))
if id != '':
if (int(id) == int(stu['id'])):
stu_search_list.append(stu)
if name != '':
if (name == stu['name']):
stu_search_list.append(stu)
show_search(stu_search_list)
is_continue = input("是否继续查找? y/n")
if (is_continue == 'y'):
continue
else:
return
else:
print("未录入学生信息")
return
def show_search(stu_search_list):
if (len(stu_search_list)):
# 标题显示格式
format_title = "{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}"
print(format_title.format("ID", "姓名", "PHP", "JAVA", "NET", "总成绩"))
# 文本内容显示格式
for item in stu_search_list:
print(format_title.format(
item['id'], item['name'], item['php'], item['java'], item['net'], int(item['php'])+int(item['java'])+int(item['net'])))
else:
print("没有该学生信息")
return
def total():
if (os_path.exists(filename)):
with open(filename, "r", encoding="utf-8") as r_file:
stu_list = r_file.readlines()
print(f"一共有{len(stu_list)}条学生信息")
else:
print("暂无学生信息")
def show():
if (os_path.exists(filename)):
stu_list = []
with open(filename, "r", encoding="utf-8") as r_file:
stu_list = r_file.readlines()
if (len(stu_list)):
format_title = "{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}"
print(format_title.format(
"ID", "姓名", "PHP", "JAVA", "NET", "总成绩"))
for item in stu_list:
stu = dict(eval(item))
print(format_title.format(stu['id'], stu['name'], stu['php'], stu['java'], stu['net'], int(
stu['php'])+int(stu['java'])+int(stu['net'])))
else:
print("没有学生信息")
else:
print("暂无学生信息")
def sort():
if (os_path.exists(filename)):
while True:
show()
with open(filename, "r", encoding="utf-8") as r_file:
type = int(input("请选择排序项,php为1,java为2,net为3,总成绩为4:"))
if (type in [1, 2, 3, 4]):
up_dows = int(input("请选择升序还是降序:1升序,2降序:"))
up_dows_bool = False
if (up_dows) == 1:
up_dows_bool = False
elif int(up_dows) == 2:
up_dows_bool = True
else:
print("输入的排序方式有误")
continue
stu_list = r_file.readlines()
stu_list_new = []
sort_list = []
for item in stu_list:
stu_list_new.append(eval(item))
if type == 1:
stu_list_new.sort(
key=lambda x: int(x['php']), reverse=up_dows_bool)
if type == 2:
stu_list_new.sort(
key=lambda x: int(x['java']), reverse=up_dows_bool)
if type == 3:
stu_list_new.sort(
key=lambda x: int(x['net']), reverse=up_dows_bool)
if type == 4:
stu_list_new.sort(
key=lambda x: int(x['php'])+int(x['java'])+int(x['net']), reverse=up_dows_bool)
show_search(stu_list_new)
is_continue = input("是否继续查询?y/n")
if is_continue == "y":
continue
else:
return
else:
print("输入有误")
continue
else:
print("暂未录入学生信息")
if (__name__ == '__main__'):
main()
Python开发简易学生管理系统
最新推荐文章于 2024-06-17 14:50:54 发布