13,420
edits
Changes
→PART 1 - Handling Errors
TypeError: Can't convert 'int' object to str implicitly
</source><br>'''Question:''' According to the exception error message, what do you think caused the error?<br><br>
::#Click on the link '[https://docs.python.org/3/library/exceptions.html#concrete-exceptions https://docs.python.org/3/library/exceptions.html#concrete-exceptions.]' and scroll or search for '''TypeError'''. Take a few moments to determine what a ''TypeError'' exception error means.<br><br>You should have learned that the TypeError exception error indicates a mismatch of a type (i.e. string, int, float, list, etc). If Python doesn't know how to handle it, perhaps we could change the number into a string or change the string into a number.<br><br>If we want to write this program safely, we can catch this error while it's happening. This is done with a specific block of code called a [https://docs.python.org/3/tutorial/errors.html#handling-exceptions '''tryclause''' clause] where you place code inbetween in-between the '''try:''' and 'the '''except:'''coding blocks. In a general sense, it works like a modified if-else statement, where the try statement acts as a test, and the except statement will or will not handle the exception depending if it occurs or does NOT occur. That is to say, If no error occurs in the code contained in the '''except portion ''' section, the script will continue as usual but if an error occurs in the except section, then it can be skippedhandled with additional coding (like an user-friendly error message).<br><br>
::#Let's demonstrate to handle our TypeError error by issuing the following lines of code:<source lang="python">
try: