学生信息管理系统python代码
时间: 2023-07-03 20:30:09 浏览: 99
数据库大作业-基于python实现学生信息管理系统+源代码+文档说明+er图+结构图
以下是一个简单的学生信息管理系统的Python代码示例:
```python
students = [] # 初始为空的学生列表
# 添加学生信息
def add_student(name, age, grade):
student = {"name": name, "age": age, "grade": grade}
students.append(student)
# 显示所有学生信息
def show_students():
for student in students:
print("Name: ", student["name"])
print("Age: ", student["age"])
print("Grade: ", student["grade"])
print()
# 搜索学生信息
def search_student(name):
for student in students:
if student["name"] == name:
print("Name: ", student["name"])
print("Age: ", student["age"])
print("Grade: ", student["grade"])
print()
return
print("Student not found.")
# 删除学生信息
def delete_student(name):
for student in students:
if student["name"] == name:
students.remove(student)
print("Student deleted.")
return
print("Student not found.")
# 更新学生信息
def update_student(name, age, grade):
for student in students:
if student["name"] == name:
student["age"] = age
student["grade"] = grade
print("Student information updated.")
return
print("Student not found.")
# 主程序
while True:
print("1. Add student")
print("2. Show all students")
print("3. Search student")
print("4. Delete student")
print("5. Update student information")
print("6. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
name = input("Enter name: ")
age = int(input("Enter age: "))
grade = input("Enter grade: ")
add_student(name, age, grade)
elif choice == 2:
show_students()
elif choice == 3:
name = input("Enter student name to search: ")
search_student(name)
elif choice == 4:
name = input("Enter student name to delete: ")
delete_student(name)
elif choice == 5:
name = input("Enter student name to update: ")
age = int(input("Enter new age: "))
grade = input("Enter new grade: ")
update_student(name, age, grade)
elif choice == 6:
break
else:
print("Invalid choice.")
```
此代码使用一个学生列表来存储所有学生信息,并提供了添加、显示、搜索、删除和更新学生信息的功能。在主程序中,用户可以选择要执行的操作。
阅读全文