weixin_44418968 2019-01-06 06:46 采纳率: 0%

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

2. get_patient 返回病人的ID
3.get_statistics 返回女病人和男病人的数量，以及平均BMI

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

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.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)
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

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.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
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
``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥100 libcurl使用无法连接服务器问题
• ¥15 链表栈表达式求值求解
• ¥15 关于龙芯1b，JTAG停止调试服务
• ¥20 收一个快手协议下单算法
• ¥15 神途脚本逐鹿神途，管理自欺欺人
• ¥15 求一个图片中的成交量选股公式
• ¥15 已知正方形内随机生成坐标matlab
• ¥30 关于#python#的问题：我想要的是这79个大特征对于房屋售价的最大的影响前十名（相关搜索：随机森林）
• ¥15 脚本运行Python文件，报错：error: unrecognized
• ¥15 使用matlab计算自定义特殊函数的二重积分，改变积分顺序所得的结果不同的问题？