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
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.
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.
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?
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.
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.
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
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]