Python Classes: Medical Insurance Project

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.

thank you so much! the review and the way u showed it made sense and I got it to work, as well as being able to do my own!

1 Like

Hi,
don’t know if this is still relevant but here’s me piece of code about try and except

try:
  patient1.update_num_children("two")
except TypeError:
  print("Incorrect operation for calculating estimated insurance cost. Please insert numerical value.")

Not sure why I’m getting an error here. Any ideas? I don’t see any syntax errors.

class Patient:

  def __init__(self, name, age, sex, bmi, num_of_children, smoker):

    self.name = name

    self.age = age

    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("{Patient Name}’s estimated insurance costs is {estimated cost} dollars.".format(self.name, estimated_cost)

patient1 = Patient("John Doe", 20, 1, 22.2, 0, 0)

patient1.estimated_insurance_cost()

 File "script.py", line 14
    patient1 = Patient("John Doe", 20, 1, 22.2, 0, 0)
           ^
SyntaxError: invalid syntax

If you find a SyntaxError but the line itself makes sense have a look at the previous lines as it’s often a propagating issue like a missing parenthesis or similar.

Hi, is there a way I let the class choose between using the first constructor (with individual inputs) and a second constructor (with list as an input) based on the user’s input? or classes can only take only constructor?

There are several ways you might achieve what I think you want in Python but I’ll try and cover what I think might be better or easier options first. Some of this might be beyond what you’ve covered in lessons so far in which case it might be best to gloss over it for now rather than try to implement it.

Since you’re just passing identical arguments as a list instead of individual arguments one option might be to just unpack your list of arguments using the unpacking operator *. This passes each element of your sequence as an individual argument. Creation of a new instance might then then look like-

patient1 = Patient(*patient_data)

where patient_data is a list in the same order as the function parameters. The main benefit of this is that you don’t have to change anything at all about your current class.

As a more generic solution when your class may be initialised from a different input source most guidance would probably push you to making use of a new classmethod instead for this special input. So __init__ remains as it is and you make a new method perhaps from_iterable or similar to take arguments from a list to construct new instances from arguments in lists. Usage might then be something like the following-

class Patient:
    # keep the basic constructor as-is
    def__init__(... 
     
    # new method to create an instance
    @classmethod
    def from_iterable(cls, data):
        return cls(*data)

patient1 = Patient.from_iterable(patient1_data)
Other options

Another option much like the first but necessitating a bit more work would be to adapt the original constructor to take an arbitrary numbers of arguments but the current constructor would need to be altered (names would be assigned based on list index which is a probably not an ideal solution, keyword arguments might be better but that’s even more changes).

What you’re kind of asking for here is function overloading (if you have a web search you’ll find a lot of information about this) where a function uses a different implementation based on the arguments it is provided. I think one of the previous options would be preferable to trying to actually overload the function as it’s not strictly supported in Python.

The simplest method for this is to use logic statements based on your inputs e.g. if type == list do something but it’s a little unusual. There is also an optional decorator that does this in a slightly neater way- functools — Higher-order functions and operations on callable objects — Python 3.10.0 documentation.

1 Like

Looks like you defined the estimated_insuance_cost() method outside the class, so your object has not this attribute. Try to define it inside the Patienr class and it should work :wink:

Should looks like this :

class Patient:
  def __init__(self, name, age, sex, bmi, num_of_children, smoker):
    self.name = name
    self.age = age
    self.sex = sex
    self.bmi = bmi
    self.num_of_children = num_of_children
    self.smoker = smoker
    # add more parameters here
  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("{name}’s estimated insurance costs is {cost} dollars.".format(name = self.name, cost = estimated_cost))
    return estimated_cost

Yes I’m able to solve this, solution similar to @breyenguyen ‘s but I’m thinking what if it’s a list of lists (a list of multiple patients’ data). Anyone? I tried for loop but unsuccesssful. I think there’s some syntax error here self.name.

class Patient2:
  def __init__(self, lst):
    for i in lst:
      self.name = lst[i][0]
      self.age = lst[i][1]
      self.sex = lst[i][2]
      self.bmi = lst[i][3]
      self.num_of_children = lst[i][4]
      self.smoker = lst[i][5]