简体   繁体   中英

Custom List of Objects

I am trying to create object of my class and append it to the list. I am using a custom class of Patient which has some properties and method to set the values of properties. So basically i want to store list of patients where each index has each patients record. When displaying values from List it is not displaying anything only some ID value like`124 Below is my code for class Patients:

 class Patients:

    def __init__(self):
        self.patient_id = ""
        self.patient_name = ""
        self.patient_age = ""
        self.patient_gender = ""
        self.patient_disease = ""

  def set_patient_values(self):
    //code to set values which user inputs like 
      self.patient_id=int(input("Please enter patient ID : "))


  //
 //appointment.py file :
    
from Patients import Patients

def AddPatient():
    list2=[]
    p1 = Patients()
    p1.set_patient_values()
    list2.append(p1)
    print(p1.patient_id)
    return list2

if __name__ == "__main__":
    list1 = []
    print("Welcome to Hospital System \n")
    Flag=True
    while Flag:
        print("Press 1. To Create Patient Record ")
        print("Press 2. To View Patient Records ")
        x=int(input("Please input from Menu :"))
        if x == 1:
            list1 = AddPatient()

        elif x==2:
            for vertex in list1:
                print(vertex.patient_id)
        else:
            break

Try read and understand this code, it is what do you want i suppose.

class Patients:
    def __init__(self):
        self.patient_id = ""
        self.patient_name = ""
        self.patient_age = ""
        self.patient_gender = ""
        self.patient_disease = ""

    def set_patient_values(self):
        self.patient_id = int(input("Please enter patient ID : "))


def AddPatient():
    pat = Patients()
    pat.set_patient_values()
    return pat


if __name__ == "__main__":
    list_patient = []
    print("Welcome to Hospital System \n")
    while True:
        x = int(input("Press 1. To Create Patient Record\nPress 2. To View Patient Records\n"))
        if x == 1:
            list_patient.append(AddPatient())
        elif x == 2 and list_patient:
            for pat in list_patient:
                print(f'patiend_id = {pat.patient_id} for object {pat}')
        else:
            break
class Patients:
    
    def __init__(self):
        self.patient_id = ""
        self.patient_name = ""
        self.patient_age = ""
        self.patient_gender = ""
        self.patient_disease = ""

    def set_patient_values(self):
        self.patient_id = int(input("Please enter patient ID : "))
        self.patient_name = str(input("Enter patient's Name:"))
        self.patient_age = int(input("Enter patient's age:"))
        self.patient_gender = str(input("Enter patient's gender:"))
        self.patient_disease = str(input("Enter patient's disease:"))

# to test the code on one individual patient : 
# tempPatient = Patients()
# tempPatient.set_patient_values()   
# patientDict[tempPatient.patient_id] = tempPatient.__dict__              

if __name__ == "__main__":
    
    patientDict = {}
    detailDict = {}
    print("Welcome to Hospital System \n")
    Flag=True
    while Flag:
        print("Press 1. To Create Patient Record ")
        print("Press 2. To View Patient Records ")
        x = int(input("Please input from Menu :"))
        if x == 1:
            tempPatient = Patients()
            tempPatient.set_patient_values()
            patientDict[tempPatient.patient_id] = tempPatient.__dict__            
                      
        elif x == 2:
            
            for vertex in patientDict:
                print(patientDict[vertex])
        else:
            break

I've made some changes that might help you:D

I added a dictionary that will contain details of the patients. You can further try formatting the output code so it's more readable.

I hope this answer was helpful:D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM