weixin_44418968 2019-01-06 06:46 采纳率: 0%
浏览 1365
已采纳

python子类继承父类时找不到__init__

UML

类PatientManagement有3个方法
1. add_patient 添加一个新病人(属于类 Patient)。病人对应一个ID,第一个ID为0,第二个为1,以此类推
2. get_patient 返回病人的ID
3.get_statistics 返回女病人和男病人的数量,以及平均BMI
病人和ID以字典的方式储存在变量 patients 中,以{id: Patient}形式,最后一个使用的ID储存在变量 last_used 中。
类 Patient 有两个方法
1. to_string 打印 名 姓 出生日 月 年 性别 身高(cm)体重(kg)
2. get_bmi 计算这个病人的BMI,BMI = weight in KG / (height in M)²

get_statistics调用get_bmi 且不再进行一变BMI计算

运行结果是这样

class Patient(object):

    def __init__(self, first_name, last_name, birth_year, birth_month, birth_day, sex, body_height, body_weight):
        self.first_name = first_name
        self.last_name = last_name
        self.year = birth_year
        self.month = birth_month
        self.day = birth_day
        self.sex = sex
        self.height = body_height
        self.weight = body_weight

    def to_string(self):
        print("Name: {} {}, geboren am {}.{}.{}, geschlecht:{}, {}cm, {}kg".format(self.first_name, self.last_name, self.day, self.month, self.year, self.sex, self.height, self.weight))

    def get_bmi(self):
        return self.weight / ((self.height/100) ** 2)


a =[]
b = []
patients = {}
i = 0
class PatientManagement(Patient):

    fpCount = 0
    mpCount = 0


    def add_patient(self):
        patients[i] = Patient()
        i += 1
        if self.sex == f:
            PatientManagement.fpCount += 1
        if self.sex == m:
            PatientManagement.mpCount += 1
        for k in patients.keys():
            a.append(k)
            b.append(patients[k])
        last_used = a[-1]

    def get_patient(self):
        new_dict = {v:k for k,v in patients.items()}
        patient_id = new_dict[Patient()]
        return patients_id

    def get_statistics(self):
        bmi = []
        for patient in b:
            bmi.append(patient.get_bmi())
        avg_bmi = np.mean(bmi)
        return "female Patients:{}, male Patients:{}, average BMI: {}".format(PatientManagement.fpCount, PatientManagement.mpCount, avg_bmi)

p1 = Patient("Meier", "Lena", 1988, 12, 12, 'f', 164, 50)
pm1 = PatientManagement(p1)
pm1.add_patient()
pm1.get_patient()

这里报错是问题在哪里?


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-141-ebb011904724> in <module>()
      1 p1 = Patient("Meier", "Lena", 1988, 12, 12, 'f', 164, 50)
----> 2 pm1 = PatientManagement(p1)
      3 pm1.add_patient()
      4 pm1.get_patient()

TypeError: __init__() missing 7 required positional arguments: 'last_name', 'birth_year', 'birth_month', 'birth_day', 'sex', 'body_height', and 'body_weight'
  • 写回答

2条回答 默认 最新

  • weixin_44418968 2019-01-08 01:16
    关注

    今天想出来了

    class Patient(object):
    
        def __init__(self, first_name, last_name, birth_year, birth_month, birth_day, sex, body_height, body_weight):
            self.first_name = first_name
            self.last_name = last_name
            self.year = birth_year
            self.month = birth_month
            self.day = birth_day
            self.sex = sex
            self.height = body_height
            self.weight = body_weight
    
        def to_string(self):
            print("Name: {} {}, geboren am {}.{}.{}, geschlecht:{}, {}cm, {}kg".format(self.first_name, self.last_name, self.day, self.month, self.year, self.sex, self.height, self.weight))
    
        def get_bmi(self):
            return self.weight / ((self.height/100) ** 2)
    
    
    
    
    class PatientManagement(Patient):
    
        fpCount = 0
        mpCount = 0
        a =[]
        b = []
        patients = {}
        i = 0
    
        def __init__(self):
            pass
    
        def add_patient(self,Patient):
            PatientManagement.patients[PatientManagement.i] = Patient
            PatientManagement.i += 1
            if Patient.sex == 'f':
                PatientManagement.fpCount += 1
            if Patient.sex == 'm':
                PatientManagement.mpCount += 1
            for k in PatientManagement.patients.keys():
                PatientManagement.a.append(k)
                PatientManagement.b.append(PatientManagement.patients[k])
            last_used = PatientManagement.a[-1]
    
        def get_patient(self,Patient):
            new_dict = {v:k for k,v in PatientManagement.patients.items()}
            patient_id = patients[i]
            return patient_id
    
        def get_statistics(self):
            bmi = []
            for patient in PatientManagement.b:
                bmi.append(patient.get_bmi())
            avg_bmi = np.mean(bmi)
            print("female Patients:{}, male Patients:{}, average BMI: {}".format(PatientManagement.fpCount, PatientManagement.mpCount, avg_bmi))
    
    patients = []
    patients.append(Patient("Meier", "Lena", 1988, 12, 12, 'f', 164, 50))
    patients.append(Patient("Müller", "Thomas", 1989, 11, 12, 'm', 180, 78))
    patients.append(Patient("Weber", "Meike", 1977, 4, 23, 'f', 164, 61))
    patients.append(Patient("Steiner", "War", 1988, 1, 13, 'm', 164, 48))
    patients.append(Patient("Lees", "Bay", 1998, 3, 16, 'f', 156, 70))
    patients.append(Patient("Rose", "Sophia", 1988, 8, 11, 'f', 164, 55))
    
    pm = PatientManagement()
    for patient in patients:
        print("Adding {} {}, bmi: {:.2f}".format(patient.first_name, patient.last_name,patient.get_bmi()))
        pm.add_patient(patient)
    
    pm.get_statistics()
    
    for i in range(len(patients)):
        print("{} {} {:.2f}".format(pm.get_patient(i).first_name, 
                                    pm.get_patient(i).last_name, 
                                    pm.get_patient(i).get_bmi()))
    

    输出结果

    Adding Meier Lena, bmi: 18.59
    Adding Müller Thomas, bmi: 24.07
    Adding Weber Meike, bmi: 22.68
    Adding Steiner War, bmi: 17.85
    Adding Lees Bay, bmi: 28.76
    Adding Rose Sophia, bmi: 20.45
    female Patients:4, male Patients:2, average BMI: 21.63
    Meier Lena 18.59
    Müller Thomas 24.07
    Weber Meike 22.68
    Steiner War 17.85
    Lees Bay 28.76
    Rose Sophia 20.45
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器