DataScience | Classes | Medical Insurance Project

DataScience Classes | Medical Insurance Project

I get invalid syntax error for line 14 and 37 but cannot find any indent or character errors.

Any ideas.

Thx
HB

There are a few things.

Look at how estimated_cost= is defined. You left out key pieces of information in the formula. Remember, you’re using class variables here so you have to use the self keyword.

estimated_cost = 250 * self.age - 128 * self.sex …etc

Also, revisit how you utilized the .format() function.
Your code: print("{0}'s estimated insurance costs are {1}".format(self.name, str(estimated_cost))

Summary

You don’t really need to use “{0}” and “{1}” b/c Python replaces the placeholders by value in order that they’re listed. You also don’t need to convert the second items to str().

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

@linguisticsconsultin,

As already mentioned, make sure you include the self keyword for all the attributes.

There actually isn’t a problem with your formatted strings, though. You’re just missing a whole bunch of closing parentheses.

Thank guys. All fixed

1 Like

Hi! Greetings!! I have a similar code but i get a “syntax error: invalid syntax” in this part of the code:

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()

I cannot understand what i’m doing wrong… please help :s

Hi, welcome to the forums.

Please post all of your formatted code.

2 Likes

Hii! :slight_smile: Here is all the code… and after all I got all the errors hehe. But I don’t know if it’s all good with the code!
I just need to do the final exercise to use “try” and “except” statements for bmi and smoker parameters.

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 are {}".format(self.name, str(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_num_Children(self, new_num_children):

self.num_of_children = new_num_children

if self.num_of_children == 1:

  print("{} has {} child.".format(self.name, str(self.num_of_children)))

else:

  print("{} has {} children.".format(self.name, str(self.num_of_children)))

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.estimated_insurance_cost()

print(patient1.patient_profile())

Thankss :))

Could you please wrap the entire code with formatting, from the first line with class to after your final print statement. You can use a three backticks before and after for this, e.g.
```
code goes here
```
Which line gives you an error? The full error code would be useful.

2 Likes

Hey, i solved the problem already! I had some () in the wrong place and the code wasn’t running correctly… Sorry for the trouble :confused:

My trouble now is to upload lists of patient data rather than just individual numbers, is it enought to put this in the code?

“patient_data = [“John Doe”, 25, 1, 22.2, 0, 0]”

or should i put:

“patient_data = [name, age, sex, bmi, num_of_children, smoker]”

Sorry for the question, but there is any tip for the resolution?

Thank you very much for your time! :slight_smile:

You’d want your original data (outside the class) in list form, aye. I think this instruction is suggesting that you should be able to create a new Patient instance in the following manner-

class Patient
    ... # rest of code


patient_data = ["John Doe", 25, 1, 22.2, 0, 0]
patient_instance = Patient(patient_data)

The idea being each individual Patient can be created simply by passing a list of data rather than writing in every piece of data as individual arguments to Patient. This would of course require a few changes within your class definition but that’s what I believe the end goal is.

1 Like

Thank you very much!! :slight_smile:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.