Tag Archives: #mastery29

Mastery 29

In this program we need a variable with a number less than 10 to be able to work with it, therefore, we will check any number that the user gives us. If he gives us a number greater than 10 the program will ask for a new number. If he gives us a number less than 10, it will be validated. After that the program can work with the variable.

Mastery 29

Mastery 29

Videotutorial de la mastery 29!


https://youtu.be/lFBDV4-oKSc

Learn To Program 2015-04-30 16:44:00

Mastery29

Validar datos en cualquier lenguaje de programación es importante, porque existen subrutinas que solo ejecutarán instrucciones con un tipo de dato y si el dato de entrada no corresponde al tipo correcto es muy posible que cuando intentemos probarlo nos genere error.
Para ello he utilizado las instrucciones try y except, mismas que conocí por la recomendación de un amigo.

**********************
Programa hecho en Python:
**********************
https://github.com/A01630323/Learn-To-Program/blob/master/Mastery29.py

Learn To Program 2015-04-30 16:44:00

Mastery29

Validar datos en cualquier lenguaje de programación es importante, porque existen subrutinas que solo ejecutarán instrucciones con un tipo de dato y si el dato de entrada no corresponde al tipo correcto es muy posible que cuando intentemos probarlo nos genere error.
Para ello he utilizado las instrucciones try y except, mismas que conocí por la recomendación de un amigo.

**********************
Programa hecho en Python:
**********************
https://github.com/A01630323/Learn-To-Program/blob/master/Mastery29.py

Learn To Program 2015-04-30 16:44:00

Mastery29

Validar datos en cualquier lenguaje de programación es importante, porque existen subrutinas que solo ejecutarán instrucciones con un tipo de dato y si el dato de entrada no corresponde al tipo correcto es muy posible que cuando intentemos probarlo nos genere error.
Para ello he utilizado las instrucciones try y except, mismas que conocí por la recomendación de un amigo.

**********************
Programa hecho en Python:
**********************
https://github.com/A01630323/Learn-To-Program/blob/master/Mastery29.py

Validated user input in Python

                                                                                                                      @PablO_CVi

This is very useless, because helps to make a good and clean code without too many error to fix, like this example:

while  True:
        try:
            q=int(input(“Please enter a non-negative integer number: “))
            break
        except ValueError:
            print(“this is not a integer number, try again: “)

 

while q<0:
        try:
            q=int(input(“this is not a positive number,try again: “))
        except ValueError:
            print (“this is not a integer number, try again:  “)
            while True:
                try:
                    q=int(input(“Please enter a non-negative integer number: “))
                    break
                except ValueError:
                    print (“this is not a integer number, try again: “)

 

validacion de input #Mastery29 #TC1014

this is the link for my video:

validacion de input #Mastery29 #TC1014

#Mastery29 #TC1014 #LaNieveDeColimaExploto #PenguinLove #video

Validacion de un input en Python espero que les guste

Suscribanse y denle like 🙂

http://youtu.be/mRO4kGKXWf8?hd=1

Mastery 29 Validated User input in Python

Sometimes in our code we want an special type of value and in this case we have to be ensure that the user enters the correct value. So with something we can be ensure, like in the next example is just for that:

while  True:
        try:
            q=int(input(“Please enter a non-negative integer number: “))
            break
        except ValueError:
            print(“this is not a integer number, try again: “)

while q<0:
        try:
            q=int(input(“this is not a positive number,try again: “))
        except ValueError:
            print (“this is not a integer number, try again:  “)
            while True:
                try:
                    q=int(input(“Please enter a non-negative integer number: “))
                    break
                except ValueError:
                    print (“this is not a integer number, try again: “)

 

In this example we want a value positive and integer. so first check that the value that the user enters has to be integer if enters an integer number the value would be through to next condition, and if is not a integer number it will be through to an except ValueError that  just when the user enter the correct value it will through the value to the next condition.

Ok let see, the next condition say that the number it has to be positive if is not, the value will be through to except ValueError again until the value enter for the users be correct.But also the user in this case can enter a positive number but no integer so we have to be ensure again of this with another while true that does the same operations of the last one.

Until the value complete all of the conditions the value will be through to  the funtion or operation that we want to do with them.

this is just an example, it depends of what value you want, you just has to write the statements of “try” and “except” in order of what you want.

 

 

Mastery 29. Validated User input in Python

There are some cases where we create a function in our program destined to receive an special type of value. Here is where we want to ensure that the user enters the correct value and not any other type that will cause our program to crash, thats why we validate the user input by using some tools that I will show you.

Lets see the next example:

In this case we want our user to enter a positive integer number.

There are 3 possible cases in this input:

a) The user enters a positive integer number: In this case, the input will be correct and Python will get out of the loop, continuinh with the rest of the code.

 

b) The user enters a negative integer number: In this case the input will be validated with the built-in type “int” function and it will not return any error, but as it is not positive, it will enter in the loop, asking the user to enter a positive number instead of a negative one.

 

c) The user does not enter an integer number: Any value different from an integer value will return “ValueError” because we are using the built-in type “int” function. As the program tries this value and returns the “ValueError” it will passes to the “except” statement, where it will tell the user his mistake and will send the user to the beginning again, entering in the “try” statement.

 

This is just an example of validating user inputs, but there may be many variants, depending on what value you want to get from the user. You can use as many different of built-in type functions as you wish, it is just matter of playing with the “try” and “except”statements.