Python Classes: Medical Insurance Project

Hi everyone,
did someone manage the try and except statements to ensure that patient data is uploaded using numerical values?
Could you please help me?
Thanks in advanced and have a great day!

Dear All,

I’m struggling with question 6 on the “Python Classes: Medical Insurance Project”. I’m getting a “AttributeError” even though I changed my answer to what the “Hints” section showed should be the correct answer.

For this questions I had to generate a update_age() method and then test out the method with the patient1 instance variable.

The hint section suggests patient1.update_age(26).

Did I miss something? Your help would be greatly appreciated.

see below:

An attribute error suggests you’ve tried to access an attribute that isn’t bound to a class. Have you made sure that your attributes are all correctly defined as part of that class?

Hint

Have a close look at the indentation. Functions that are designed to be methods of that class should be part of the body of that class.

You have indeed several problems with indentations.
I would recommend to start for the top to the bottom line of your code.

And then, to fix your attribute error, I would suggest you to take a look on the review’s part (nb.14) of the Classes chapter.
You will find the correct way to call the estimated_cost function.

Thank you so much! Mainly the indentations and a small issue with the update_age formula needed rectifying. It works great now.

Thank you so much! It works great now.

Hi I am struggling with the last part of the project: 14 - Update the class so that users can upload lists of patient data rather than just individual numbers.

I would update the init method as follows:

class Patient:
  def __init__ (self):
    patient_data = []
    patient_data["name"] = self.name
    patient_data["age"] = self.age
    patient_data["sex"] = self.sex
    patient_data["bmi"] = self.bmi
    patient_data["num_of_children"] = self.num_of_children
    patient_data["smoker"] = self.smoker
    return patient_data

and then call the method in the end:

patient_data = ["john", 28, 1, 0, 17.8, 1]

patient_obj = Patient(patient_data)

But I get

ypeError: init() takes 1 positional argument but 2 were given

Remember that your method will always have the instance object passed first: (self or whatever you choose to name it). Look at your __init__ then and consider how many arguments you are actually passing when you call the Patient class.

Once that’s fixed you’ll need to work out a way to assign the elements of your list to the instance attributes you want.

I’m rather confused by the current syntax, there’s a mixture of lists, dictionaries and unexpected return statement where there shouldn’t be one. Each of those things may break your code at this point so please double check your syntax here.

1 Like

I got it:


class Patient:
  def __init__(self, info):
    self.name = info[0]
    self.age = info[1]
    self.sex = info[2]
    self.bmi = info[3]
    self.num_of_children = info[3]
    self.smoker = info[4]

And then calling it in the end with

patient1 = Patient(["John", 16, 1, 22, 2, 0])
print(patient1.patient_profile())

1 Like

Hi!
Can someone please help me with why I am getting the following syntax error from my code?
File “script.py”, line 14
patient1 = Patient(“John Doe”, 25, 1, 22.2, 0, 0)
^
SyntaxError: invalid syntax

Hi robizz161. I’m getting the same error. Did you end up solving it?

For syntax errors if you’ve checked the line where this error pops up for the correct syntax then the next step is checking the preivous lines. Continutations like parantheses or line breaks can lead to an error on one line only being picked up on a later line. Pretty sure the replies to that query covered the same issue.

Oh I see. Thank you! I was so caught up with it saying the error was on line 14. It ended up being a missed )

1 Like

There should be one more parameter, which is the list of data.
Inside the function, I used the list index to assign each element to the variable. I don’t think yours is gonna work because you can’t access data in a list using [key] - this is for dictionary.

class Patient:
  def __init__(self, patient_data):
    self.name = patient_data[0]
    self.age = patient_data[1]
    self.sex = patient_data[2]
    self.bmi = patient_data[3]
    self.num_of_children = patient_data[4]
    self.smoker = patient_data[5]

Hey All,

I’m doing the Insurance project and keep getting the error NameError: name ‘insurance_cost’ is not defined. I’m not sure where I’m going wrong

# Create calculate_insurance_cost() function below: 
def calculate_insurance_cost(age, sex, bmi, num_of_children, smoker):
 estimated_cost = 250*age - 128*sex + 370*bmi + 425*num_of_children + 24000*smoker - 12500
 print("The estimated insurance cost for this person is " + str(estimated_cost) + "dollars.")
 return estimated_cost

# Initial variables for Maria 
age = 28
sex = 0  
bmi = 26.2
num_of_children = 3
smoker = 0  

# Estimate Maria's insurance cost
def estimated_insurance_cost(Maria):
 maria_insurance_cost = calculate_insurance_cost(age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0) 

 print("The estimated insurance cost for Maria is " + str(insurance_cost) + " dollars.")
 return insurance_cost

# Initial variables for Omar
age = 35
sex = 1 
bmi = 22.2
num_of_children = 0
smoker = 1  

# Estimate Omar's insurance cost 
def estimated_insurance_cost(Omar):
 omar_insurance_cost = calculate_insurance_cost(age = 35, sex = 1, bmi = 22.2, num_of_children = 3, smoker = 1) 

 print("The estimated insurance cost for Maria is " + str(insurance_cost) + " dollars.")
 return insurance_cost

print("The estimated insurance cost for Omar is " + str(insurance_cost) + " dollars.")

I don’t think you actually assign anything to that name, i.e. there is no statement like insurance_cost = 3 which would be an assignment. You have similar names like maria_insurance_cost, is that what you meant to use?

I think I meant to use Maria_insurance cost, etc. Thank you,
Now when I change the names over I get no results.
‘’’

Create calculate_insurance_cost() function below:

def calculate_insurance_cost(age, sex, bmi, num_of_children, smoker):
estimated_cost = 250age - 128sex + 370bmi + 425num_of_children + 24000*smoker - 12500
print("The estimated insurance cost for this person is " + str(estimated_cost) + “dollars.”)
return estimated_cost

Initial variables for Maria

age = 28
sex = 0
bmi = 26.2
num_of_children = 3
smoker = 0

Estimate Maria’s insurance cost

def estimated_insurance_cost(Maria):
maria_insurance_cost = calculate_insurance_cost(age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0)

print(“The estimated insurance cost for Maria is " + str(maria_insurance_cost) + " dollars.”)
return maria_insurance_cost

Initial variables for Omar

age = 35
sex = 1
bmi = 22.2
num_of_children = 0
smoker = 1

Estimate Omar’s insurance cost

def estimated_insurance_cost(Omar):
omar_insurance_cost = calculate_insurance_cost(age = 35, sex = 1, bmi = 22.2, num_of_children = 3, smoker = 1)

print(“The estimated insurance cost for Maria is " + str(omar_insurance_cost) + " dollars.”)
return omar_insurance_cost

‘’’

It’s hard to read without formatting, please see the following- How do I format code in my posts? and consider editing your code to update it.

Do you call any of the functions you define? The code within the body of a function would not execute unless you explicitly ran that function.

1 Like

I think I’ve called them. I’ve formatted the code properly I think i used </> and pasted

# Create calculate_insurance_cost() function below: 

def calculate_insurance_cost(age, sex, bmi, num_of_children, smoker):
 estimated_cost = 250*age - 128*sex + 370*bmi + 425*num_of_children + 24000*smoker - 12500
 print("The estimated insurance cost for this person is " + str(estimated_cost) + "dollars.")
 return estimated_cost

# Initial variables for Maria 
age = 28
sex = 0  
bmi = 26.2
num_of_children = 3
smoker = 0  

# Estimate Maria's insurance cost
def estimated_insurance_cost(Maria):
 maria_insurance_cost = calculate_insurance_cost(age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0) 

 print("The estimated insurance cost for Maria is " + str(maria_insurance_cost) + " dollars.")
 return maria_insurance_cost

# Initial variables for Omar
age = 35
sex = 1 
bmi = 22.2
num_of_children = 0
smoker = 1  

# Estimate Omar's insurance cost 
def estimated_insurance_cost(Omar):
 omar_insurance_cost = calculate_insurance_cost(age = 35, sex = 1, bmi = 22.2, num_of_children = 3, smoker = 1) 

 print("The estimated insurance cost for Maria is " + str(omar_insurance_cost) + " dollars.")
 return omar_insurance_cost


The definition of a function looks like-

def func(parameter):
    return parameter

Actually calling the function would be-

func(3)

It should be the function name followed by parentheses containing the arguments to pass to the function.

I think it might be worth a quick recap of Python’s functions to make sure you’ve not missed something as functions are so important to Python you’d want to absolutely master the basics.