用python写一个可以录入学生信息、录入学生成绩、查询学生信息、查询学生成绩、修改学生成绩、删除学生成绩的系统
时间: 2023-07-10 18:33:27 浏览: 108
好的,我会为你提供一个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 set_age(self, age):
self.age = age
def set_gender(self, gender):
self.gender = gender
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 set_age(self, name, age):
if name in self.students:
self.students[name].set_age(age)
def set_gender(self, name, gender):
if name in self.students:
self.students[name].set_gender(gender)
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. Set Age")
print("6. Set Gender")
print("7. Get Info")
print("8. 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: ")
age = input("Enter age: ")
management.set_age(name, age)
print("Age updated successfully!")
elif choice == "6":
name = input("Enter name: ")
gender = input("Enter gender: ")
management.set_gender(name, gender)
print("Gender updated successfully!")
elif choice == "7":
name = input("Enter name: ")
info = management.get_info(name)
if info is None:
print("Student not found!")
else:
print(info)
elif choice == "8":
break
else:
print("Invalid choice!")
```
这段代码中,`Student`类表示一个学生,包含姓名、年龄、性别以及成绩信息。`StudentManagement`类表示学生管理系统,包含添加学生、删除学生、录入成绩、查询成绩、查询学生信息、修改学生年龄和性别等功能。在主程序中,使用一个循环来接收用户的输入,根据用户的选择执行相应的操作。你可以根据自己的需求进行修改和扩展。
阅读全文