请用Python写一个200行的学生管理系统
时间: 2023-11-17 15:26:39 浏览: 67
用python写的学生管理系统
好的,我将为您提供一个简单的Python学生管理系统代码,它大约需要200行。
```python
import csv
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
class StudentDatabase:
def __init__(self):
self.students = []
self.load_students()
def load_students(self):
try:
with open('students.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
name = row[0]
age = int(row[1])
grade = float(row[2])
student = Student(name, age, grade)
self.students.append(student)
except FileNotFoundError:
print('No existing student database file found.')
def save_students(self):
with open('students.csv', 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
for student in self.students:
name = student.name
age = student.age
grade = student.grade
csv_writer.writerow([name, age, grade])
def add_student(self, name, age, grade):
student = Student(name, age, grade)
self.students.append(student)
self.save_students()
print('Student added successfully.')
def remove_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
self.save_students()
print('Student removed successfully.')
break
else:
print('Student not found.')
def view_students(self):
if len(self.students) == 0:
print('No students found.')
return
for student in self.students:
print('Name:', student.name)
print('Age:', student.age)
print('Grade:', student.grade)
print()
def main():
database = StudentDatabase()
while True:
print('Welcome to the Student Management System.\n')
print('1. Add a student')
print('2. Remove a student')
print('3. View all students')
print('4. Quit')
choice = input('\nEnter your choice: ')
if choice == '1':
name = input('Enter student name: ')
age = int(input('Enter student age: '))
grade = float(input('Enter student grade: '))
database.add_student(name, age, grade)
elif choice == '2':
name = input('Enter student name: ')
database.remove_student(name)
elif choice == '3':
database.view_students()
elif choice == '4':
print('Goodbye!')
break
else:
print('Invalid choice. Please try again.')
if __name__ == '__main__':
main()
```
这个学生管理系统主要使用了csv模块来读写学生信息,包括学生姓名,年龄和成绩。用户可以使用系统来添加,删除和查看所有学生。该系统还使用了类和对象,以及异常处理和循环。虽然这个代码可能不完美,但它可以为您提供一个基本的学生管理系统的框架,您可以根据自己的需求进行修改和改进。
阅读全文