Validating User Input in Python

--Originally published at Ed_Alita

 

User do not know how to use a computer

This mentally you need to have in order to have a great code.

Lets see if I have the next code:

validating

If the user gave you for any reason a letter because he or she is stupid the code will crash:

validating-2gif

In order to prevent this we are going to validate what the user gave us is corect in this case first we are going to obtain the input without any type we do this by puting raw_input.

the next thing is to do the text if it is a number with int(User_Input). since we know that if this obtain a number it will crash we are going to do a cheat to stop this.

We use Try and except:

What try does it proves the code and Except will do an action if the codecrash.

The complete code will look like this:

validating3

In this case if the user gave us a string it should make this:

validating4

If the user gave us a integer it should run smothly:

validating5

Go and Have fun as a Code Master