An initial example of Input Error Handling in Python

Crystal X
CodeX
Published in
2 min readJun 5, 2021

--

As I have progressed in my studies of Python programming, I have begun to study error handling, which is important to ensure the robustness of a script. One type of error is an input error, which is an error that the user makes when he is inputting data.

I have included the code for a short script I have recently written. The script below is a very small program that will be able to tell the user whether an entered number is odd or even.

The user is prompted to enter a number and this number is converted from a string to an integer.

If the number is divisible by 2 then a message is printed saying it is an even number, otherwise a message is printed saying it is an odd number is printer.

The thing that is wrong with the below code is the fact there are no failsafe mechanisms put in place to ensure that only the desired data is entered:-

The code below is an adaptation to add an input error catching mechanism.

The program has now become a while loop. As long as the variable num is ‘y’ then the loop will operate.

Error handling code has been added to the program: The user is asked to enter a number and if it is an integer, a message saying it is odd or even will be printed out depending on whether the…

--

--

Crystal X
CodeX

I have over five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.