用Python实现学生信息管理系统
概述
本文将讲述如何用Python实现一个简易的学生信息管理系统。
该系统包括以下功能:
- 添加学生信息
- 删除学生信息
- 修改学生信息
- 查询学生信息
数据结构
我们可以用一个列表来存储所有学生的信息,列表中的每个元素都代表一个学生的信息,包括姓名、性别、年龄、学号等。
例如:
students = [
{"name": "Tom", "gender": "Male", "age": 18, "student_id": "1001"},
{"name": "Jerry", "gender": "Female", "age": 19, "student_id": "1002"},
{"name": "Alice", "gender": "Female", "age": 20, "student_id": "1003"},
{"name": "Bob", "gender": "Male", "age": 21, "student_id": "1004"}
]
添加学生信息
要添加一个学生的信息,可以让用户输入学生的姓名、性别、年龄、学号等信息,然后创建一个字典,将这些信息存储到字典中,最后将字典添加到列表中即可。
def add_student():
name = input("Please enter student's name:")
gender = input("Please enter student's gender:")
age = input("Please enter student's age:")
student_id = input("Please enter student's ID:")
student = {"name": name, "gender": gender, "age": age, "student_id": student_id}
students.append(student)
print("Student added successfully!")
删除学生信息
要删除一个学生的信息,可以让用户输入学生的学号,然后遍历列表,找到对应学号的学生信息,将其删除即可。
def delete_student():
student_id = input("Please enter student's ID:")
for student in students:
if student["student_id"] == student_id:
students.remove(student)
print("Student deleted successfully!")
return
print("Student not found!")
修改学生信息
要修改一个学生的信息,可以让用户输入学生的学号,然后遍历列表,找到对应学号的学生信息,让用户输入要修改的信息,然后更新字典即可。
def modify_student():
student_id = input("Please enter student's ID:")
for student in students:
if student["student_id"] == student_id:
student["name"] = input("Please enter student's name:")
student["gender"] = input("Please enter student's gender:")
student["age"] = input("Please enter student's age:")
print("Student modified successfully!")
return
print("Student not found!")
查询学生信息
要查询学生的信息,可以让用户输入学生的姓名或学号,然后遍历列表,找到对应的学生信息,将其显示出来。
def search_student():
keyword = input("Please enter student's name or ID:")
for student in students:
if student["name"] == keyword or student["student_id"] == keyword:
print(f"Name: {student['name']}, Gender: {student['gender']}, Age: {student['age']}, ID: {student['student_id']}")
return
print("Student not found!")
完整示例
以下是完整代码示例:
students = [
{"name": "Tom", "gender": "Male", "age": 18, "student_id": "1001"},
{"name": "Jerry", "gender": "Female", "age": 19, "student_id": "1002"},
{"name": "Alice", "gender": "Female", "age": 20, "student_id": "1003"},
{"name": "Bob", "gender": "Male", "age": 21, "student_id": "1004"}
]
def add_student():
name = input("Please enter student's name:")
gender = input("Please enter student's gender:")
age = input("Please enter student's age:")
student_id = input("Please enter student's ID:")
student = {"name": name, "gender": gender, "age": age, "student_id": student_id}
students.append(student)
print("Student added successfully!")
def delete_student():
student_id = input("Please enter student's ID:")
for student in students:
if student["student_id"] == student_id:
students.remove(student)
print("Student deleted successfully!")
return
print("Student not found!")
def modify_student():
student_id = input("Please enter student's ID:")
for student in students:
if student["student_id"] == student_id:
student["name"] = input("Please enter student's name:")
student["gender"] = input("Please enter student's gender:")
student["age"] = input("Please enter student's age:")
print("Student modified successfully!")
return
print("Student not found!")
def search_student():
keyword = input("Please enter student's name or ID:")
for student in students:
if student["name"] == keyword or student["student_id"] == keyword:
print(f"Name: {student['name']}, Gender: {student['gender']}, Age: {student['age']}, ID: {student['student_id']}")
return
print("Student not found!")
while True:
print("Please select an operation:")
print("1. Add student")
print("2. Delete student")
print("3. Modify student")
print("4. Search student")
print("5. Exit")
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:
break
else:
print("Invalid choice, please try again.")
示例说明
示例1:添加学生信息
- 用户选择添加学生信息功能
- 用户输入学生信息,例如:姓名:张三,性别:男,年龄:18,学号:1001
- 系统提示学生添加成功
- 查看学生列表,会发现列表中新增了一条学生信息:
{"name": "张三", "gender": "男", "age": "18", "student_id": "1001"}
示例2:查询学生信息
- 用户选择查询学生信息功能
- 用户输入学生姓名或学号,例如:张三
- 系统会在列表中查找姓名为张三的学生,如果找到则会显示学生的详细信息:
Name: 张三, Gender: 男, Age: 18, ID: 1001
如果找不到则会提示学生不存在。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:用python实现学生信息管理系统 - Python技术站