请写出医院问诊系统的Python代码
时间: 2024-02-05 17:12:21 浏览: 28
以下是一个简单的医院问诊系统的 Python 代码示例:
```python
# 医院问诊系统
# 定义患者类
class Patient:
def __init__(self, name, age, symptoms):
self.name = name
self.age = age
self.symptoms = symptoms
def __str__(self):
return f"{self.name}, {self.age} 岁, 症状: {self.symptoms}"
# 定义医生类
class Doctor:
def __init__(self, name, specialty):
self.name = name
self.specialty = specialty
def diagnose(self, patient):
# 根据症状做出诊断
diagnosis = "未知"
if "发热" in patient.symptoms:
diagnosis = "感冒"
elif "头痛" in patient.symptoms:
diagnosis = "偏头痛"
elif "腹痛" in patient.symptoms:
diagnosis = "胃炎"
return diagnosis
# 定义问诊系统类
class Hospital:
def __init__(self):
self.patients = []
self.doctors = []
def add_patient(self, patient):
self.patients.append(patient)
def add_doctor(self, doctor):
self.doctors.append(doctor)
def diagnose_patient(self, patient_name, doctor_name):
# 找到患者和医生
patient = None
for p in self.patients:
if p.name == patient_name:
patient = p
break
doctor = None
for d in self.doctors:
if d.name == doctor_name:
doctor = d
break
if not patient or not doctor:
print("未找到患者或医生")
return
# 做出诊断
diagnosis = doctor.diagnose(patient)
print(f"{patient.name} 的诊断结果为: {diagnosis}")
# 测试代码
if __name__ == "__main__":
# 创建医院和医生
hospital = Hospital()
doctor1 = Doctor("张医生", "感冒专家")
doctor2 = Doctor("李医生", "头痛专家")
doctor3 = Doctor("王医生", "胃病专家")
hospital.add_doctor(doctor1)
hospital.add_doctor(doctor2)
hospital.add_doctor(doctor3)
# 创建患者并添加到医院
patient1 = Patient("张三", 30, ["发热"])
patient2 = Patient("李四", 25, ["头痛"])
patient3 = Patient("王五", 40, ["腹痛"])
hospital.add_patient(patient1)
hospital.add_patient(patient2)
hospital.add_patient(patient3)
# 进行诊断
hospital.diagnose_patient("张三", "张医生")
hospital.diagnose_patient("李四", "李医生")
hospital.diagnose_patient("王五", "王医生")
```
这个例子中,我们定义了三个类:Patient、Doctor 和 Hospital。Patient 类表示患者,Doctor 类表示医生,Hospital 类表示医院。我们在 Hospital 类中定义了一些方法,如添加患者、添加医生和进行诊断等。
在测试代码中,我们创建了一些患者和医生,并将它们添加到医院中。然后,我们调用 diagnose_patient 方法来进行诊断。该方法会找到患者和医生,并调用医生的 diagnose 方法来做出诊断。最后,我们将诊断结果输出到控制台上。