Try and Except on Medical Insurance Project

First post on the forum so please be kind!!

I am trying the extra tasks on the medical insurance project as part of the classes topic

Here is the link to my code:

I have added the try and except approach to making sure all inputs are integers as far as I can see what I have coded works if the age is input incorrectly but if I change any other parameters whilst still leaving the age parameter as an integer it doesn’t come up with an error code.

What am I doing wrong?

Which bit is troubling you, if you’re just checking whether or not a single attribute is an integer then changing other parameters whilst age is an integer shouldn’t throw any error codes.

Within the estimated_cost function you check for a TypeError in the calculation but that could be any of the attributes, if you want to just check age then do that first. It’s also worth noting that Python is quite happy to perform math with types other than integers, e.g. floats or complex numbers, see-

At the moment though you only check the age at initialisation. Since you have a method to update age perhaps it would be wise to test it there?

Apologies if I’m missing the mark on your actual query so please add a little more information and an example of what you did and what you expected to happen or something along those lines.

No need to apologise and thank you for coming back to me.

Let me talk you what I did.

patient2 = Patient(“John Doe”, “25”, 1, 22.2, 0, 0) comes up with the type error which is all good as I am using a string as one of the parameters which should be an integer .

However I would also expect
patient2 = Patient(“John Doe”, 25, 1, “22.2”, 0, 0) to produce the type error as I am using a string for the bmi but it doesn’t.

Can you see from my code why it doesn’t?

Hope that makes sense.

The type error is only raised when you call the estimated_insurance_cost function. Since you are only initialising an instance of the class, the code that checks if bmi is an integer is never run.