Python如何关联患者与疫苗两类以实现疫苗接种凭证功能
疫苗接种凭证程序实现方案
原有代码基础问题修正
- 类构造方法必须使用双下划线包裹的
__init__,单下划线写法不会被识别为构造方法,实例化时会直接报错 - 原
Vaccination类职责定义混乱,参数数量和实例化时传入的参数不匹配,需重新梳理类职责 - 原代码存在重复定义
address属性、方法未写完的低级错误
关联逻辑说明
无需额外新增类即可实现患者和疫苗信息的关联:直接在Patient患者类中新增vaccine_records列表属性,每完成一次接种就往该列表中插入对应接种记录,即可完成二者的绑定。如果需要单独维护接种相关的扩展信息,也可以新增VaccinationRecord接种记录类做中间关联,结构更清晰。
完整终端演示代码
# 疫苗类:存储疫苗基础信息 class Vaccine: def __init__(self, vaccine_id, name, manufacturer): self.vaccine_id = vaccine_id self.name = name self.manufacturer = manufacturer # 接种记录类:存储单次接种的相关信息,关联疫苗和接种时间、地点 class VaccinationRecord: def __init__(self, vaccine, vaccinate_date, address): self.vaccine = vaccine self.vaccinate_date = vaccinate_date self.address = address # 患者类:存储用户基础信息,关联自身所有接种记录 class Patient: def __init__(self, patient_id, name, birth_date, gender, phone, address): self.patient_id = patient_id self.name = name self.birth_date = birth_date self.gender = gender self.phone = phone self.address = address # 关联接种记录的列表,实现患者和接种信息的绑定 self.vaccine_records = [] # 新增接种记录 def add_vaccination_record(self, record): self.vaccine_records.append(record) print(f"已为{self.name}成功登记{record.vaccine.name}疫苗接种记录") # 核验是否持有有效接种凭证(可自行调整核验规则,比如判断接种剂次、是否在有效期内) def check_vaccine_cert(self): if len(self.vaccine_records) > 0: print(f"\n===== 接种凭证核验结果 =====") print(f"患者ID:{self.patient_id} | 姓名:{self.name}") print("持有有效接种凭证,接种记录如下:") for idx, record in enumerate(self.vaccine_records, 1): print(f"{idx}、疫苗名称:{record.vaccine.name} | 接种日期:{record.vaccinate_date} | 接种地点:{record.address}") return True else: print(f"患者{self.name}(ID:{self.patient_id})暂无接种记录,无有效接种凭证") return False # 全局存储所有患者信息,模拟数据库 all_patients = {} # 预制疫苗示例 az_vaccine = Vaccine("V001", "阿斯利康", "阿斯利康制药") sinovac_vaccine = Vaccine("V002", "科兴", "北京科兴中维") if __name__ == "__main__": while True: print("\n===== 疫苗接种凭证管理系统 =====") print("1、录入患者信息") print("2、登记患者接种记录") print("3、核验接种凭证") print("4、退出系统") choice = input("请输入操作编号:") if choice == "1": patient_id = input("请输入患者身份证/ID号:") if patient_id in all_patients: print("该ID患者已存在,无需重复录入") continue name = input("请输入患者姓名:") birth_date = input("请输入患者出生日期(格式示例:1990-01-01):") gender = input("请输入患者性别:") phone = input("请输入患者联系电话:") address = input("请输入患者居住地址:") patient = Patient(patient_id, name, birth_date, gender, phone, address) all_patients[patient_id] = patient print(f"患者{name}信息录入成功") elif choice == "2": patient_id = input("请输入要登记接种记录的患者ID:") if patient_id not in all_patients: print("未查询到该ID对应的患者信息,请先录入患者信息") continue print("可选疫苗列表:1、阿斯利康(V001) 2、科兴(V002)") vac_choice = input("请输入接种疫苗的编号:") vaccine = az_vaccine if vac_choice == "1" else sinovac_vaccine vac_date = input("请输入接种日期(格式示例:2021-06-23):") vac_address = input("请输入接种地点:") record = VaccinationRecord(vaccine, vac_date, vac_address) all_patients[patient_id].add_vaccination_record(record) elif choice == "3": patient_id = input("请输入要核验的患者ID:") if patient_id not in all_patients: print("未查询到该ID对应的患者信息") continue all_patients[patient_id].check_vaccine_cert() elif choice == "4": print("退出系统成功") break else: print("输入的操作编号无效,请重新输入")
功能说明
运行代码后按照终端提示操作即可,支持录入患者基础信息、为患者登记接种记录、输入患者ID核验接种凭证三个核心功能,核验规则可根据需求自行调整,比如增加接种剂次要求、有效期判断等。
内容的提问来源于stack exchange,提问作者Autchman
相关产品推荐
相关产品推荐

