Forum Archive

And object to list when it’s created.

resserone13

I’ve been haven’t trouble adding the patient to a list when the object is created.


master_patient_list = []
current_patient_list = []
iso_patient_list = []

def count_patients(list):
        return len(list)

class Patient (object):

        def __init__(self, name=None, age=None, room_number=None, assigned_doctor=None, assigned_nurse=None, illness=None, iso_status=None, patient_record_nunber=None,patient_medications=[]):
                self.name = name
                self.age = age 
                self.room_number = room_number
                self.assigned_doctor = assigned_doctor
                self.assigned_nurse = assigned_nurse
                self.illness = illness
                self.iso_status = iso_status
                self.patient_reord_number = patient_record_nunber
                self.patient_medications = list(patient_medications)
                self.add_patient()

        def add_patient(self):
                if self.name not in master_patient_list and current_patient_list:
                        master_patient_list.append(self.name)
                        current_patient_list.append(self.name)

        def remove_patient():
                pass

        def move_patient(self, new_room):
                self.room_number = new_room         

        def add_med(self, med):
                if med not in self.patient_medications:
                        self.patient_medications.append(med)

        def remove_med(self, med):
                if med in self.patient_medications:
                        self.patient_medications.remove(med)        


        def __str__(self):
                return f'Room: {self.room_number} \nName: {self.name} \nPhysician: {self.assigned_doctor} \nNurse: {self.assigned_nurse} \nIllness: {self.illness} \nIso status: {self.iso_status} \nPRN: {self.patient_reord_number} \nMedication: {self.patient_medications}' 


#patient info               
patient1 = Patient()
patient1.name = 'Sally'
patient1.age = 35
patient1.room_number = 430
patient1.assigned_doctor = 'Dr. Seuss'
patient1.assigned_nurse = 'Jessica'
patient1.illness = 'Broken Leg'
patient1.iso_status = 'NONE'
patient1.patient_reord_number = 'AD123456789'
patient1.add_med('norco')

#test code
print(patient1)

print(f'Master list: {master_patient_list}')
print(f'Current list: {current_patient_list}')

count_patients(current_patient_list)



mikael

@resserone13:

  • This will never be true in your code: if self.name not in master_patient_list and current_patient_list: because current_patient_list is always empty, which means False in Python, and thus never added to, and thus stays empty.
  • You should not store the patient’s name in the list (several patients can have the same name). Just store the patient object itself.
ccc

Just like you created a def __str__(self):, you can also create a def __len__(self): as discussed at
https://docs.python.org/3/reference/datamodel.html#object.__len__.

resserone13

@mikael thanks for the feedback. I always appreciate it. I see now how checking the list the way I am isn’t going to work. The master list will have every patient who’s ever been at the hospital and the current list will only have current patients. That along with how I was storing them by name. With all this in mind I will try to figure something else out and let you know how it goes. Thanks