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

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条回答 默认 最新

相关推荐 更多相似问题