如何用python写一个可以录入学生信息、录入学生成绩、查询学生信息、查询学生成绩、修改学生成绩、删除学生成绩的系统
时间: 2023-07-10 19:33:27 浏览: 81
你可以使用Python语言来实现一个简单的学生成绩管理系统,以下是一个示例代码:
```python
class Student:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
self.grades = {}
def add_grade(self, subject, grade):
self.grades[subject] = grade
def get_grade(self, subject):
return self.grades.get(subject)
def get_info(self):
return f"Name: {self.name}, Age: {self.age}, Gender: {self.gender}"
class StudentManagement:
def __init__(self):
self.students = {}
def add_student(self, student):
self.students[student.name] = student
def remove_student(self, name):
if name in self.students:
del self.students[name]
def add_grade(self, name, subject, grade):
if name in self.students:
self.students[name].add_grade(subject, grade)
def get_grade(self, name, subject):
if name in self.students:
return self.students[name].get_grade(subject)
def get_info(self, name):
if name in self.students:
return self.students[name].get_info()
management = StudentManagement()
while True:
print("1. Add Student")
print("2. Remove Student")
print("3. Add Grade")
print("4. Get Grade")
print("5. Get Info")
print("6. Exit")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter name: ")
age = input("Enter age: ")
gender = input("Enter gender: ")
student = Student(name, age, gender)
management.add_student(student)
print("Student added successfully!")
elif choice == "2":
name = input("Enter name: ")
management.remove_student(name)
print("Student removed successfully!")
elif choice == "3":
name = input("Enter name: ")
subject = input("Enter subject: ")
grade = input("Enter grade: ")
management.add_grade(name, subject, grade)
print("Grade added successfully!")
elif choice == "4":
name = input("Enter name: ")
subject = input("Enter subject: ")
grade = management.get_grade(name, subject)
if grade is None:
print("Grade not found!")
else:
print(f"Grade for {name} in {subject}: {grade}")
elif choice == "5":
name = input("Enter name: ")
info = management.get_info(name)
if info is None:
print("Student not found!")
else:
print(info)
elif choice == "6":
break
else:
print("Invalid choice!")
```
这段代码中,`Student`类表示一个学生,包含姓名、年龄、性别以及成绩信息。`StudentManagement`类表示学生管理系统,包含添加学生、删除学生、录入成绩、查询成绩、查询学生信息等功能。在主程序中,使用一个循环来接收用户的输入,根据用户的选择执行相应的操作。你可以根据自己的需求进行修改和扩展。
阅读全文