Python Try:Except:

Hi all,

The extra challenge for Medical Insurance Project in Python consisted in the following:

  • Use try and except statements to ensure that patient data is uploaded using numerical values.

Here below is my code for the assignment. I haven’t quite understood where to place my try: except: lines in order to govern the user input used to create the objects. Thanks in advance!

Could you link the lesson/project, unfortunately there are several projects that share that name.

It sounds like something you’d want to check either when the value is first bound to the object or when it’s updated (perhaps both). I suppose it depends a little on what you want to happen, should this throw an error and crash the program or should it warn and ask for alternative input.

Sure thing, thank you!

This is the link to the specific mini-project:

Pretty much the need is to warn the user when the parameters used to create a new patient record are not equivalent to the expected data types, for example if they input an int as the name or a str as the age.

Hope that helps!

If it specifies creation then you don’t need to worry about updating (though by all means add it in if you want). It sounds like you’d want to be checking the argument before it is first used. What’s the first bit of code that you make use of the arguments you passed to the class? You’d went to test either at or before that point.

Thank you! Got it to work with raise but will try also will try: except:

It worked!

Thanks again!

1 Like

Try except is deliberately catching a specific error so it’s up to you to decide what happens if that error does occur. You’ve already done one where the error is raised but an alternative could skip this data entry and write to a log for example; set the value to None or something and carry on; prompt manually set the age with input or something; or, don’t create this patient at all (a little more work).

The best option would vary depending on what this code is used for, which isn’t really applicable in this case, but you can always play around :slightly_smiling_face:.

1 Like

Yeah definitely. Felt like I needed at least a little sample of try except before moving on with my path on Codecademy. I am learning having fun and I love that. Cheers!

1 Like