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.

101 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time: 2023/11/21 10:04
# @Author:LoverZZr
# @FiLe:StudentManage.py
# @Project:demo
# @Software:PyCharm
import json
# 学生字典,用于保存学生信息
students = {}
# 添加学生信息
def add_student():
name = input("请输入学生姓名: ")
id = input("请输入学生学号: ")
phone = input("请输入学生手机号: ")
sex = input("请输入学生性别")
students[id] = {"姓名": name, "性别": sex, "学号": id, "手机号": phone}
print(f"学生信息添加成功: {name},{sex}, {id}, {phone}")
# 查询学生信息
def query_student():
id = input("请输入要查询的学生学号: ")
if id in students:
print(
f"学生信息: (姓名:{students[id]['姓名']},性别:{students[id]['性别']},学号: {id}, 手机号:{students[id]['手机号']})")
else:
print("没有找到该学生信息")
# 修改学生信息
def modify_student():
id = input("请输入要修改的学生学号: ")
if id in students:
name = input("请输入新的学生姓名: ")
phone = input("请输入新的学生手机号: ")
sex = input("请输入新的学生性别: ")
students[id] = {"姓名": name, "性别": sex, "学号": id, "手机号": phone}
print(f"学生信息修改成功: {name},{sex}, {id}, {phone}")
else:
print("没有找到该学生信息")
# 删除学生信息
def delete_student():
id = input("请输入要删除的学生学号: ")
if id in students:
del students[id]
print(f"学生信息删除成功: {id}")
else:
print("没有找到该学生信息")
# 显示所有学生信息
def show_students():
for student in students.values():
print(f"姓名: {student['姓名']},性别: {student['性别']} ,学号: {student['学号']},手机号: {student['手机号']}")
# 保存所有学生信息到一个JSON文件
def save_students():
with open("students.json", "w", encoding="utf-8") as f:
json.dump(students, f, ensure_ascii=False)
# 通过序号来选择需要使用到的功能
def main():
while True:
print("请输入项目序号选择要执行的操作:")
print("1. 添加学生信息")
print("2. 查询学生信息")
print("3. 修改学生信息")
print("4. 删除学生信息")
print("5. 显示学生信息")
print("6. 保存学生信息")
print("7. 退出系统")
choice = input("请输入操作编号: ")
if choice == "1":
add_student()
elif choice == "2":
query_student()
elif choice == "3":
modify_student()
elif choice == "4":
delete_student()
elif choice == "5":
show_students()
elif choice == "6":
save_students()
elif choice == '7':
break
else:
print("无效选择,请重新选择")
if __name__ == "__main__":
main()