使用Python写一个学生管理系统
时间: 2023-03-10 16:27:39 浏览: 27
好的,以下是一个简单的使用Python编写的学生管理系统:
```
students = []
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
def __str__(self):
return f"Name: {self.name}\nAge: {self.age}\nGrade: {self.grade}"
def add_student():
print("Add Student")
name = input("Enter name: ")
age = input("Enter age: ")
grade = input("Enter grade: ")
student = Student(name, age, grade)
students.append(student)
print("Student added successfully!")
def view_student():
print("View Student")
for student in students:
print(student)
print("---------------------")
def delete_student():
print("Delete Student")
name = input("Enter name: ")
for i, student in enumerate(students):
if student.name == name:
students.pop(i)
print("Student deleted successfully!")
return
print("Student not found!")
def main():
while True:
print("\nStudent Management System\n")
print("1. Add Student")
print("2. View Students")
print("3. Delete Student")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == "1":
add_student()
elif choice == "2":
view_student()
elif choice == "3":
delete_student()
elif choice == "4":
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main()
```
这个学生管理系统包括添加、查看和删除学生的功能。程序启动时,它会打印一个菜单,让用户选择要执行的操作。选择“添加学生”会提示用户输入学生的姓名、年龄和成绩,并将其添加到系统中。选择“查看学生”会列出所有已添加的学生。选择“删除学生”会提示用户输入要删除的学生的姓名,并从系统中删除该学生。最后,选择“退出”将退出程序。
阅读全文