1.

Solve : Input validation?

Answer»

Hi,
I've just started LEARNING PROGRAMMING using python and our tutor told us to always validate our INPUT but I have no idea what that really means or how I would go about doing this.  I am an absolute beginner in programming so a very simple answer would be a gr8 help.
Thx.   [smiley=embarassed.gif] Quote

Hi,
I've just started learning programming using python and our tutor told us to always validate our input but I have no idea what that really means or how I would go about doing this.  I am an absolute beginner in programming so a very simple answer would be a gr8 help.
Thx.   [smiley=embarassed.gif]
input, AFAIK, can mean the input arguments to a function/procedure or the script arguments, or user input. To validate means to check them according to your predefined set of "rules". For example to get the user to key in a number (because you wanted them to) when they run your script, you can use the raw_input() and isdigit() function like this
CODE: [Select]userinput = raw_input("Please ENTER a number: " )
if userinput.isdigit():  # <-- this is considered a validation.
   print "You entered a number, good"
else:
   ...blah...
The above example stores what the user keys in into variable userinput. Then the isdigit() method checks whether its a digit. This is called validation. Its the same for other types of inputs.
Thank you so much Ghostdog74,
That has helped alot.


Discussion

No Comment Found