定义一个学员类,包括以下字段(学员编号,姓名,入学时间,数据库考试成绩、UML考试成绩、上机考试成绩),根据学员的总成绩实现一些若干功能。
时间: 2023-05-24 10:05:51 浏览: 70
class Student:
def __init__(self, sid, name, enroll_date, db_score, uml_score, coding_score):
self.sid = sid
self.name = name
self.enroll_date = enroll_date
self.db_score = db_score
self.uml_score = uml_score
self.coding_score = coding_score
def total_score(self):
return self.db_score + self.uml_score + self.coding_score
def get_grade(self):
total = self.total_score()
if total >= 90:
return "A"
elif total >= 80:
return "B"
elif total >= 70:
return "C"
elif total >= 60:
return "D"
else:
return "F"
def time_to_graduation(self):
now = datetime.datetime.now()
enroll_date = datetime.datetime.strptime(self.enroll_date, '%Y-%m-%d')
diff = int((now - enroll_date).days / 365)
return 4 - diff
def __str__(self):
return f"{self.sid}, {self.name}, {self.enroll_date}, {self.db_score}, {self.uml_score}, {self.coding_score}"
# example usage
s = Student(1, "John", "2020-09-01", 80, 85, 90)
print(s.total_score()) # 255
print(s.get_grade()) # A
print(s.time_to_graduation()) # 3
阅读全文