Hello, I’m learning about classes in Python and I"m having trouble with this exercise. It’s not printing the expected result
So, if we run the code in your codebyte, the output is
From that, I’d suspect that you intended the
estimated_insurance_cost function to be included in the class. It isn’t. Can you see why not?
You have an outdented line of code between the class definition and the function. Python uses indentation to determine scope. Your outdented line of code should probably be after the function. That would include the function in the class where it belongs.
Hello, thank you so much for getting back to me! I realized the issue with patient1 once you brought my attention to it so I’ve put it at the end so that estimated_insurance_cost stays in the class but now I’m getting a message that my print isn’t working because self isn’t defined which I don’t understand because i’ve defined it in "def estimated_insurance_cost(self)
The issue is again with indentation.
self refers to the current instance of the class, so references to
self need to be inside the class as well. In this case it looks as though your
I see, I see. I’ve also notice I hadn’t put a \ before the 's in my print statement so I’ve added that and now I’m not getting error messages but i’m also not getting anything printed and I don’t see why.
The code you’ve shared produces no errors or output. What additional code did you run?
Some things I see that are problematic with what you’ve shared:
You are creating an instance of your
Patient class inside the class itself inside the
estimated_insurance_cost function. You probably meant for that to be outside of the class.
patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0)
Then in your print statement, you are calling the function at the end of it:
print(self.name + "\'s estimated insurance cost is " + str(estimated_cost) + " dollars.", patient1.estimated_insurance_cost()) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is an example of recursion. In this case it causes a stack overflow error because the function will repeatedly call itself forever. You’ll learn about recursion later (if you haven’t already). It has its uses, but this is not a case where you want the function to call itself.
I’m assuming that what you want here is to create an instance of
Patient and then call the
estimated_insurance_cost function on that instance. Those tasks should occur outside of the class at the end of your code.
I figured it out!!! Thank you so much.
I’m trying to update the class so that users can upload lists of patient data rather than just individual numbers but i’m having some issues. I"m getting "AttributeError: ‘list’ object has no attribute ‘update_age’. Would be grateful if you could check it out1
Consider what you did here:
When you try to execute this line:
patient1.update_age=["John Doe", 46, 1, 22.2, 0, 0]
patient1 refer to? It’s still a list:
patient1 = ["John Doe", 25, 1, 22.2, 0, 0]
update_num_children are methods. The way you wrote them, they each accept a single parameter. How do you call such a method?
I’ve copied, and run your code. It works well once the issues above are addressed. Nice job!
You might consider giving your class a
__str__ method for easy printing of each Patient instance rather than your current
My update was succeful! Thanks!
I was able to modify it and to make it print the updated age or other info but my patient_profile dictionary doesn’t work. I wasn’t able to make it read the list. I believe a regular instance method only takes one argument
Actually it works fine, but you need to call an instance method on an instance of the class.
patient1 is not an instance of
Patient. Compare your last 2 lines of code. In the first
num_of_children property of the
john. In the last
Patient class on a list rather than on the