You can´t do that

--Originally published at Coding with jared

Today´s blog is about validation of the user input, before starting with this topic, we need to know what is data validation.

data validation: Making sure that a program operates based on clean, correct and useful data.

data validation in programming is needed to continue procedures, for example if a program is asking for an integer, and a the user gives a string as answer the program, may crashed or send an error message that the answer given is nott a correct option in order to continue the program.

The errors are handled with try and except.

Try works as the follow

try: ´´´ Instructions …´´´

except : ´´´Ínstructins…´´´

first all the instructions from try to except are executed, but if no exception is find that block ends and now we continue to the block except.

For example, in this program of my calculator in Spanish, i am giving the user the option to select an what operations he/she wants to do, but there are only four options, numbered from 1 to 4, and to select it you just have to select the number and press enter, but what if the user is a smart ass and instead of give as answer the given options he/she decides to enter another number…

Screenshot from 2016-11-20 19-06-11.png

know i am able, to outsmart the smartass

 

Screenshot from 2016-11-20 19-12-21.png

 

This is where I learned about validations, try and except statements :9

https://docs.python.org/3/tutorial/errors.html