我是Python编码的初学者,我尝试学习抽象类。我在实现两个扩展抽象类考试的考试时遇到了问题,这两个考试定义了诊断测试的契约。
class Person:
def __init__(self, name: str, age: int):
self.name = name
self.age = age
class Patient(Person):
def __init__(self, name: str, age: int, id: str, symptoms: list\[str\], cholesterol: float, iron_level: float):
super().__init__(name, age)
self.id = id
self.symptoms = symptoms
self.cholesterol = cholesterol
self.iron_level = iron_level\`
class CholesterolExam(Exam):
def __init__(self):
super().__init__('Cholesterol Exam')
def perform_exam(self,cholesterol_exam):
if cholesterol_exam >= 200:
return False
return True
class IronLevelExam(Exam):
def __init__(self):
super().__init__('Iron Exam')
def perform_exam(self,iron_level_exam):
if iron_level_exam>50 and iron_level_exam<170:
return True
class Test03(unittest.TestCase):
def test_doctor(self):
cholesterol_exam = CholesterolExam()
iron_level_exam = IronLevelExam()
p = Patient('John', 30, '001', ['cough', 'fever'], 180, 300)
cholesterol_exam_result, iron_exam_result = [exam.perform_exam(p) for exam in [cholesterol_exam, iron_level_exam]]
self.assertTrue(cholesterol_exam_result)
self.assertFalse(iron_exam_result)
test(Test03)
我收到错误消息:
test_doctor中的第32行
胆固醇检查结果,铁水平检查结果=[胆固醇检查中的检查性能检查(p)]
第12行,执行考试
如果胆固醇检查>=200:
类型错误:“>=”“Patient”和“int”的实例之间不支持