Python Classes: Medical Insurance Project

hi, this is my code in medical insurance project


it prints “John Doe’s estimated insurance costs is 1836.0 dollars.
None”
can you explain me why there is “none”

Hello! It is because the estimated_insurance_cost function has no return. That means when you print the return value:

print(patient1.estimated_insurance_cost())

You are printing None, as there is no return value. The reason you still get the John Doe’s estimated insurance costs is 1836.0 dollars. printed is because you still call the function when you print it, which means any print statements inside it get printed.

2 Likes

thanks
i replaced print() in method to return, and it starts work without "none’

2 Likes

You don’t need the print when you call the method. Since the method has the print statement in it already, you can call the method itself, and it will print. Using print is why you get none, as there is nothing “returned”.
“print(patient1.estimated_insurance_cost())” will print what is returned, which is “none”.

Instead, remove the print, and just use “patient1.estimated_insurance_cost()”. This will run the method, which will initiate the print in the method itself, instead of printing the none that is returned.

It’s odd.
My code looks identical, but it returns with an attribution error. Can you help?

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("{}'s estimated insurance costs is {} dollars.".format(self.name, estimated_cost))
patient1.estimated_insurance_cost()


Traceback (most recent call last):
  File "C:\Users\Aston\.spyder-py3\temp.py", line 27, in <module>
    print(patient1.estimated_insurance_cost())
AttributeError: 'Patient' object has no attribute 'estimated_insurance_cost'

@phreakoutaston Did you define patient1 ie: John Doe?

Is this all of your code?

So far, yes. Not finished.
Stuck on patient1.estimated_insurance_cost(). Not sure why I am getting AttributeError.

Oh. I see. Still Attribute error.

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

#test the __init__ method
#create first instance variable
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)

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("{}'s estimated insurance costs is {} dollars.".format(self.name, estimated_cost))
patient1.estimated_insurance_cost()

Traceback (most recent call last):

  File "C:\Users\Aston\Google Drive\IT training\Codacademy\Data Science career track\working_with_objects_medical_insurance.py", line 25, in <module>
    patient1.estimated_insurance_cost()

AttributeError: 'Patient' object has no attribute 'estimated_insurance_cost'

You have an issue with indentation for that function def. of estimated_insurance_cost

Why isn’t it inside the class?

Also, you need to move the variable patient1 outside the method before you call the method and pass the argument through it.

That seems to solve the problem.
Thanks :slightly_smiling_face:

1 Like

Sorry. One more question.

Would I need to indent this? I seem to be coming up with either an indentation error (when I indent) or an attribute error (when I don’t).

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


#first method: estimate insurance cost
    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("{}'s estimated insurance costs is {} dollars.".format(self.name, estimated_cost))

def update_age(self, new_age):
    self.age = new_age
    print("{} is now {} years old.".format(self.name, str(self.age)))
    self.estimated_insurance_cost()

patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
patient1.update_age(26)


Error message when I indent def update_age(self, new_age):

runfile('C:/Users/Aston/Google Drive/IT training/Codacademy/Data Science career track/working_with_objects_medical_insurance.py', wdir='C:/Users/Aston/Google Drive/IT training/Codacademy/Data Science career track')
  File "C:\Users\Aston\Google Drive\IT training\Codacademy\Data Science career track\working_with_objects_medical_insurance.py", line 30
    def update_age(self, new_age):
    ^
IndentationError: unexpected indent```

All of the methods need to be defined under the Patient class.

Not sure how. After fixing some spaces, it seems to work now.

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


#first method: estimate insurance cost
    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("{}'s estimated insurance costs is {} dollars.".format(self.name, estimated_cost))

    def update_age(self, new_age):
        self.age = new_age
        print("{} is now {} years old.".format(self.name, str(self.age)))
        self.estimated_insurance_cost()

    def update_bmi(self, new_bmi):
        self.bmi = new_bmi
        print("{}'s BMI is now {}.".format(self.name, str(self.bmi)))
        self.estimated_insurance_cost()

patient1 = Patient("John Doe", 20, 1, 22.2, 0, 0)
patient1.update_age(26)
patient1.update_bmi(17.2)

b/c methods are defined inside the class.

1 Like

Hi all, I’m not sure if this is a bug with the console as I keep getting a syntax error on my code below (this is my first post so apologies if the format is poor!).

Error message:

File “script.py”, line 16
patient1 = Patient(“John Doe”, 25, 1, 22.2, 0, 0)
^
SyntaxError: invalid syntax

Code:

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


patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
patient1.estimated_insurance_cost()


Double check this section here (above) also…

Especially the use of parens following the definition of estimated_cost. Are they necessary? Because with parens in math…there is an order of operations and again, is that really necessary here?
Also, your print statement needs to be a return statement and double check how you wrote it. Hint: “{}” As well as what’s inside the parens after .format()

https://www.geeksforgeeks.org/python-format-function/

2 Likes

Thanks for the quick response, much appreciated!

I spotted the missing “)” and the incorrect double quotations in my attempt to specify values for placeholder {}.

Out of interest, any idea why the code threw a syntax error in the line:

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

Rather than highlighting that the error was in the esimtated_insurance_cost method?

It’s not specifically that line of code that’s causing the error. It’s what I pointed out above it that’s throwing the error.

it’s these lines that are incorrect:

estimated_cost = ((250 * self.age)-(128 * self.sex)+(370 * self.bmi)+(425*self.num_of_children)+(24000*self.smoker)- 12500)
    print("{Patient}’s estimated insurance costs is {estimated cost} dollars.".format("Patient" = self.name, "estimated cost" = str(estimated_cost))

the definition of estimated_cost does not need parentheses. And then you need to change print to return and correct the errors in how you used the .format() function.

https://github.com/el-mn/public/blob/main/medical_insurance.py

Hello, I’m wondering if there’s a less “brute force” way to create the dictionnary in the last function “patient profile”?