用python写一个学生管理系统代码
时间: 2023-12-14 14:04:05 浏览: 70
好的,以下是一个简单的学生管理系统的 Python 代码示例:
```python
students = []
class Student:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def display(self):
print("Name:", self.name)
print("Age:", self.age)
print("Gender:", self.gender)
def add_student():
name = input("Enter student name: ")
age = input("Enter student age: ")
gender = input("Enter student gender: ")
student = Student(name, age, gender)
students.append(student)
print("Student added successfully!")
def display_students():
for student in students:
student.display()
while True:
print("\nStudent Management System")
print("1. Add Student")
print("2. Display Students")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == '1':
add_student()
elif choice == '2':
display_students()
elif choice == '3':
print("Exiting...")
break
else:
print("Invalid choice! Please try again.")
```
该代码定义了一个类 `Student`,用于表示学生信息,包括姓名、年龄和性别。然后定义了一个 `add_student()` 函数,用于添加学生信息到 `students` 列表中。还定义了一个 `display_students()` 函数,用于在控制台上显示所有学生的信息。最后,使用一个 `while` 循环来提供菜单选项,允许用户添加学生、显示学生信息或退出系统。
阅读全文