源代码:
#定义学校成员类
class SchoolMember:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def get_info(self):
return f"Name: {self.name}, Age: {self.age}, Sex: {self.sex}"
#定义教师类
class Teacher(SchoolMember):
def __init__(self, name, age, sex, title, salary, prize):
super().__init__(name, age, sex)
self.title = title
self.salary = salary
self.prize = prize
def get_total_income(self):
return self.salary + self.prize
def get_info(self):
return f"Name: {self.name}, Age: {self.age}, Sex: {self.sex}, Title: {self.title}, Salary: {self.salary},prize:{self.prize} Total Income: {self.get_total_income()}"
#定义学生类
class Student(SchoolMember):
def __init__(self, name, age, sex, dept, score):
super().__init__(name, age, sex)
self.dept = dept
self.score = score
def get_info(self):
print(member)
return f"Name: {self.name}, Age: {self.age}, Sex: {self.sex}, Department: {self.dept}, Score: {self.score}"
#添加成员信息函数
# member=[]
def add_member():
member_type = input("Add a teacher or student? ")
if member_type.lower() == "teacher":
string1=input("请依次输入姓名,年龄,性别,职位,工资,奖金,用中文逗号隔开:")
array1 = string1.split(',')
try:
teacher = Teacher(array1[0], array1[1], array1[2], array1[3], array1[4], array1[5])
members.append(teacher)
except ValueError:
print("输入错误")
print("Teacher added successfully!")
elif member_type.lower() == "student":
string1 = input("请依次输入姓名,年龄,性别,所属系部、成绩用中文逗号隔开:")
array1 = string1.split(',')
print(array1)
name = array1[0]
age = array1[1]
sex = array1[2]
dept = array1[3]
score = float(array1[4])
student = Student(name, age, sex, dept, score)
members.append(student)
print("Student added successfully!")
else:
print("格式不对,请从新输入!")
#注销成员信息函数
def remove_member():
name = input("Enter the name of the member you want to remove: ")
for member in members:
if member.name == name:
members.remove(member)
print(f"{member.name} removed successfully!")
return
print("Member not found!")
#主程序部分
members = []
while True:
choice = input("请输入add或remove或info或end操作: ")
if choice == "add":
add_member()
elif choice == "remove":
remove_member()
elif choice == "info":
for member in members:
print(member.get_info())
elif choice == "end":
break
else:
print("无效操作")
注意看这个代码是要用中文逗号隔离开,但是因为搜狗输入法的这个功能——
智能调整数字后标点,让本来输入的中文逗号变成了英文逗号,还好我第二次运行的时候就注意到了,不然这bug改一天都改不出来。输入的时候逗号会自动变化,发现了这个有点离谱的问题。分享一下。