You have been asked to develop a system that makes it easier to organize patient data. You will create a class
that does the following:
Let's get started!
If you look at the code block below, you will see that we have started a class
called Patient
. It currently has an __init__
method with two class variables: self.name
and self.age
.
Let's start by adding in some more patient parameters:
sex
: patient's biological identification, 0 for male and 1 for femalebmi
: patient BMInum_of_children
: number of children patient hassmoker
: patient smoking status, 0 for a non-smoker and 1 for a smokerAdd these into the __init__
method so that we can use them as we create our class methods.
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
Let's test out our __init__
method and create our first instance variable.
Create an instance variable outside of our class called patient1
.
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
Next, print out the name of patient1
using the following line of code:
print(patient1.name)
Print out the rest of patient1
's information to ensure the __init__
method is functioning properly.
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
print(patient1.name)
print(patient1.age)
print(patient1.sex)
print(patient1.bmi)
print(patient1.num_of_children)
print(patient1.smoker)
John Doe 25 1 22.2 0 0
Now that our constructor is built out and ready to go, let's start creating some methods! Our first method will be estimated_insurance_cost()
, which takes our instance's parameters (representing our patient's information) and returns their expected yearly medical fees.
Below the __init__
constructor, define our estimated_insurance_cost()
constructor which only takes self
as an argument. Inside of this method, add the following formula:
$$ estimated\_cost = 250*age - 128*sex + 370*bmi + 425*num\_of\_children + 24000*smoker - 12500 $$
Note that we are using class variables in our formula here, so be sure to remember to use the self
keyword.
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
Inside of our estimated_insurance_cost()
method, let's add a print statement that displays the following:
{Patient Name}'s estimated insurance costs is {estimated cost} dollars.
Then, test out this method using the patient1
instance variable.
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
patient1.estimated_insurance_cost()
John Doe's estimated insurance costs is 1836.0 dollars.
We already have one super useful method in our class! Let's add some more and make our Patient
class even more powerful.
What if our patient recently had a birthday? Or had a fluctuation in weight? Or had a kid? Let's add some methods that allow us to update these parameters and recalculate the estimated medical fees in one swing.
First, create an update_age()
method. It should take in two arguments: self
and new_age
. In this method reassign self.age
to new_age
.
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
def update_age(self, new_age):
self.age = new_age
Let's flesh out this method some more!
Add a print statement that outputs the following statement:
{Patient Name} is now {Patient Age} years old.
Test out your method using the patient1
instance variable.
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
def update_age(self, new_age):
self.age = new_age
print(self.name + " is now " + str(self.age) + " years old.")
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
patient1.update_age(26)
John Doe is now 26 years old.
We also want to see what the new insurance expenses are. Call the estimated_insurance_cost()
method in update_age()
using this line of code:
self.estimated_insurance_cost()
Test out your method with patient1
.
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
def update_age(self, new_age):
self.age = new_age
print(self.name + " is now " + str(self.age) + " years old.")
self.estimated_insurance_cost()
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
patient1.update_age(26)
John Doe is now 26 years old. John Doe's estimated insurance costs is 2086.0 dollars.
Let's make another update method that modifies the num_of_children
parameter.
Below the update_age()
method, define a new one called update_num_children()
. This method should have two arguments, self
and new_num_children
. Inside the method, self.num_of_children
should be set equal to new_num_children
.
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
def update_age(self, new_age):
self.age = new_age
print(self.name + " is now " + str(self.age) + " years old.")
self.estimated_insurance_cost()
def update_num_children(self, new_num_children):
self.num_of_children = new_num_children
Similarly to the method we wrote before, let's add in a print statement that clarifies the information that is being updated.
Your print statement should output the following:
{Patient Name} has {Patient's Number of Children} children.
Use the patient1
instance variable to test out this method. Set the new_num_children
argument to 1
. Do you notice anything strange in the output?
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
def update_age(self, new_age):
self.age = new_age
print(self.name + " is now " + str(self.age) + " years old.")
self.estimated_insurance_cost()
def update_num_children(self, new_num_children):
self.num_of_children = new_num_children
print(self.name + " has " + str(self.num_of_children) + " children.")
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
patient1.update_age(26)
patient1.update_num_children(1)
John Doe is now 26 years old. John Doe's estimated insurance costs is 2086.0 dollars. John Doe has 1 children.
You may have noticed our output is grammatically incorrect because John Doe only has 1
child. Let's update our method to accurately convey when we should use the noun "children" versus when we should use "child".
To do this we can use control flow.
If the patient has 1
offspring, we should see the following output:
{Patient Name} has {Patient Number of Children} child.
Otherwise, we should see this output:
{Patient Name} has {Patient Number of Children} children.
Write out your control flow program, and test it out using patient1
.
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
def update_age(self, new_age):
self.age = new_age
print(self.name + " is now " + str(self.age) + " years old.")
self.estimated_insurance_cost()
def update_num_children(self, new_num_children):
self.num_of_children = new_num_children
if self.num_of_children == 1:
print(self.name + " has " + str(self.num_of_children) + " child.")
else:
print(self.name + " has " + str(self.num_of_children) + " children.")
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
patient1.update_age(26)
patient1.update_num_children(1)
John Doe is now 26 years old. John Doe's estimated insurance costs is 2086.0 dollars. John Doe has 1 child.
To finish off the update_num_children()
method, let's call our estimated_insurance_cost()
method at the end.
Use patient1
to ensure that everything is functioning as expected!
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
def update_age(self, new_age):
self.age = new_age
print(self.name + " is now " + str(self.age) + " years old.")
self.estimated_insurance_cost()
def update_num_children(self, new_num_children):
self.num_of_children = new_num_children
if self.num_of_children == 1:
print(self.name + " has " + str(self.num_of_children) + " child.")
else:
print(self.name + " has " + str(self.num_of_children) + " children.")
self.estimated_insurance_cost()
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
patient1.update_age(26)
patient1.update_num_children(1)
John Doe is now 26 years old. John Doe's estimated insurance costs is 2086.0 dollars. John Doe has 1 child. John Doe's estimated insurance costs is 2511.0 dollars.
Let's create one last method that uses a dictionary to store a patient's information in one convenient variable. We can use our parameters as the keys and their specific data as the values.
Define a method called patient_profile()
that builds a dictionary called patient_information
to hold all of our patient's information.
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
def update_age(self, new_age):
self.age = new_age
print(self.name + " is now " + str(self.age) + " years old.")
self.estimated_insurance_cost()
def update_num_children(self, new_num_children):
self.num_of_children = new_num_children
if self.num_of_children == 1:
print(self.name + " has " + str(self.num_of_children) + " child.")
else:
print(self.name + " has " + str(self.num_of_children) + " children.")
self.estimated_insurance_cost()
def patient_profile(self):
patient_information = {}
patient_information["Name"] = self.name
patient_information["Age"] = self.age
patient_information["Sex"] = self.sex
patient_information["BMI"] = self.bmi
patient_information["Number of Children"] = self.num_of_children
patient_information["Smoker"] = self.smoker
return patient_information
Let's test out our final method! Use patient1
to call the method patient_profile()
.
Remember that in patient_profile()
we used a return statement rather than a print statement. In order to see our dictionary outputted, we must wrap a print statement around our method call.
class Patient:
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
self.age = age
# add more parameters here
self.sex = sex
self.bmi = bmi
self.num_of_children = num_of_children
self.smoker = smoker
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
def update_age(self, new_age):
self.age = new_age
print(self.name + " is now " + str(self.age) + " years old.")
self.estimated_insurance_cost()
def update_num_children(self, new_num_children):
self.num_of_children = new_num_children
if self.num_of_children == 1:
print(self.name + " has " + str(self.num_of_children) + " child.")
else:
print(self.name + " has " + str(self.num_of_children) + " children.")
self.estimated_insurance_cost()
def patient_profile(self):
patient_information = {}
patient_information["Name"] = self.name
patient_information["Age"] = self.age
patient_information["Sex"] = self.sex
patient_information["BMI"] = self.bmi
patient_information["Number of Children"] = self.num_of_children
patient_information["Smoker"] = self.smoker
return patient_information
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
patient1.update_age(26)
patient1.update_num_children(1)
print(patient1.patient_profile())
John Doe is now 26 years old. John Doe's estimated insurance costs is 2086.0 dollars. John Doe has 1 child. John Doe's estimated insurance costs is 2511.0 dollars. {'Name': 'John Doe', 'Age': 26, 'Sex': 1, 'BMI': 22.2, 'Number of Children': 1, 'Smoker': 0}
Congratulations! You have successfully made a powerful Patient
class! Classes are an incredibly useful programming tool because they allow you to create a blueprint that can be used to build many objects off of. In this case, you can organize any patient's information and apply all methods of Patient
to update and arrange their data.
There are endless possibilities for extending the capabilities of this class. If you would like to continue to work on this Patient
class, take a look at the following challenges:
update_bmi()
or update_smoking_status()
.try
and except
statements to ensure that patient data is uploaded using numerical values.Happy coding!
class Patient:
patient_list = []
def __init__(self, name, age, sex, bmi, num_of_children, smoker):
self.name = name
try:
self.age = int(age)
except:
print("Invalid age input: Age input should be a number.")
# add more parameters here
try:
self.sex = int(sex)
except:
print("Invalid sex input: Sex input should be a number.")
try:
self.bmi = float(bmi)
except:
print("Invalid BMI input: BMI input should be a number.")
try:
self.num_of_children = int(num_of_children)
except:
print("Invalid number of children input: Number of children input should be a number.")
try:
self.smoker = int(smoker)
except:
print("Invalid smoking status input: Smoking status input should be a number.")
self.patient_list.append(self)
def estimated_insurance_cost(self):
estimated_cost = 250*self.age - 128*self.sex + 370*self.bmi + 425*self.num_of_children + 24000*self.smoker - 12500
print(self.name + "'s estimated insurance costs is " + str(estimated_cost) + " dollars.")
def update_age(self, new_age):
self.age = new_age
print(self.name + " is now " + str(self.age) + " years old.")
self.estimated_insurance_cost()
def update_num_children(self, new_num_children):
self.num_of_children = new_num_children
if self.num_of_children == 1:
print(self.name + " has " + str(self.num_of_children) + " child.")
else:
print(self.name + " has " + str(self.num_of_children) + " children.")
self.estimated_insurance_cost()
def update_bmi(self, new_bmi):
self.bmi = new_bmi
print(self.name + "'s BMI is now " + str(self.bmi) + ".")
self.estimated_insurance_cost()
def update_smoking_status(self, new_smoking_status):
self.smoker = new_smoking_status
if self.smoker == 0:
print(self.name + " is now a non-smoker.")
else:
print(self.name + " is now a smoker.")
self.estimated_insurance_cost()
def patient_profile(self):
patient_information = {}
patient_information["Name"] = self.name
patient_information["Age"] = self.age
patient_information["Sex"] = self.sex
patient_information["BMI"] = self.bmi
patient_information["Number of Children"] = self.num_of_children
patient_information["Smoker"] = self.smoker
return patient_information
@classmethod
def patient_profile_list(cls):
return [patient.patient_profile() for patient in cls.patient_list]
patient1 = Patient("John Doe", 25, 0, 22.2, 0, 0)
patient2 = Patient("Ali", 22, 0, 23, 0, 0)
patient3 = Patient("Abu", 21, 0, 28, 0, 0)
patient4 = Patient("Ahmad", 24, 0, 22.2, 0, 0)
patient5 = Patient("Rajesh", 27, 0, 22.2, 0, 0)
patient6 = Patient("Ah Ching", 23, 0, 22.2, 0, 0)
patient_profiles = Patient.patient_profile_list()
print(patient_profiles)
[{'Name': 'John Doe', 'Age': 25, 'Sex': 0, 'BMI': 22.2, 'Number of Children': 0, 'Smoker': 0}, {'Name': 'Ali', 'Age': 22, 'Sex': 0, 'BMI': 23.0, 'Number of Children': 0, 'Smoker': 0}, {'Name': 'Abu', 'Age': 21, 'Sex': 0, 'BMI': 28.0, 'Number of Children': 0, 'Smoker': 0}, {'Name': 'Ahmad', 'Age': 24, 'Sex': 0, 'BMI': 22.2, 'Number of Children': 0, 'Smoker': 0}, {'Name': 'Rajesh', 'Age': 27, 'Sex': 0, 'BMI': 22.2, 'Number of Children': 0, 'Smoker': 0}, {'Name': 'Ah Ching', 'Age': 23, 'Sex': 0, 'BMI': 22.2, 'Number of Children': 0, 'Smoker': 0}]