You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.7 KiB

import os
import json # Import json module for better file handling
from openpyxl import workbook
# 学生信息字典
student_info = {}
def print_menu():
print('-' * 30)
print('1. 添加学员信息')
print('2. 删除学员信息')
print('3. 修改学员信息')
print('4. 查询学员信息')
print('5. 显示所有学员信息')
print('6. 保存信息')
print('7. 生成excel表格')
print('8. 退出系统')
print('-' * 30)
def add_student():
# 添加学员信息
name = input('请输入学员姓名:')
student_id = input('请输入学员ID:')
gender = input('请输入学员性别:')
phone = input('请输入联系方式:')
student_info[student_id] = {'name': name, 'id': student_id, 'gender': gender, 'phone': phone}
print('学员信息添加成功!')
def delete_student():
# 删除学员信息
student_id = input('请输入要删除的学员ID:')
if student_id in student_info:
del student_info[student_id]
print('删除学员信息成功!')
else:
print('该ID不存在,删除失败!')
def modify_student():
# 修改学员信息
student_id = input('请输入要修改的学员ID:')
if student_id in student_info:
name = input('请输入新的姓名:')
phone = input('请输入新的联系方式:')
student_info[student_id]['name'] = name
student_info[student_id]['phone'] = phone
print('学员信息修改成功!')
else:
print('该ID不存在,修改失败!')
def search_student():
# 查询学员信息
student_id = input('请输入要查询的学员ID:')
if student_id in student_info:
print(student_info[student_id])
else:
print('该ID不存在!')
def print_all():
# 显示所有学员信息
print('所有学员信息如下:')
for student_id, info in student_info.items():
print(f'ID: {id}, 姓名: {info["姓名"]}, 性别: {info["性别"]}, 联系方式: {info["手机号"]}')
def save_info():
# 保存信息到文件
try:
with open('student_info.txt', 'w', encoding='utf-8') as f:
json.dump(student_info, f, ensure_ascii=False, indent=4)
print('信息保存成功!')
except Exception as e:
print(f'信息保存失败! Error: {e}')
def load_info():
# 从文件加载信息
if os.path.exists('student_info.txt'):
try:
with open('student_info.txt', 'r', encoding='utf-8') as f:
data = f.read()
student_info.update(json.loads(data))
print('信息加载成功!')
except Exception as e:
print(f'信息加载失败! Error: {e}')
# def save_info(,num):
# fn=r"学生数据表.xlsx"
# wb=workbook.Workbook()
# ws=wb.worksheets[0]
# ws.title="学生信息"
# ws.append(["id","姓名","性别","手机号"])
# for i in range(num):
# ws.append(ulist[i])
# wb.save(fn)
def main():
load_info() # Load existing information from file at the beginning
while True:
print_menu()
choice = int(input('请选择操作:'))
if choice == 1:
add_student()
elif choice == 2:
delete_student()
elif choice == 3:
modify_student()
elif choice == 4:
search_student()
elif choice == 5:
print_all()
elif choice == 6:
save_info()
elif choice == 7:
break
else:
print('输入错误,请重新输入!')
if __name__ == '__main__':
main()