EgonDev

March 2, 2010

Some Validation

Filed under: What I Did Today — Tags: — egoncasteel @ 10:33 pm

I started in web programing and have found that you can never underestimate ether the users stupidity or malicious intent. That means validation on every piece of info needs to be done. In the end you will be glad you added it even thou it is annoying and boring to program.

Now there isn’t really a good reason other then the practice to add validation to the exercises you do while going through a programing book, but it just seems wrong to take input and not validate to me. So here are a couple of ways I have started doing it in Python

Pick your option

Here is a common validation task you will see a lot. The user must choose form a list of options.

print "temp conversion program"
conType = "none"
while not(conType == "ctf" or conType == "ftc"):
    conType = raw_input("cel to fahr. or fahr to cel (enter ctf or ftc): ")
sTemp = "none"

So in this chunk of code the user is being asked if he wants to go from cel to fahr. or fahr. to cel.  I only want to except ctf or ftc. Any thing else will mess up the rest of the program.

Notice I set the conType var before the while loop that holds the prompt for the user. This is so the program will ask the use for input on the first time through. Now if the user inputs one of the chosses I want to see the while loop will eval false and the program will move on. If the user enters something I don’t want to see the while loop will eval true and give the user a chance to try again.

Getting the right type of input

Here is another validation problem that comes up a lot. Getting the right type of input. In this case the “type” I am talking about is data type (str, int, float, …).

change = "none"
while change == "none":
    try:
        change = float(raw_input("enter the change: "))
    except:
        print "** input must be a float **"
        change = "none"
        pass

In this code sample I am trying to get the user to give me something I can turn in to a float (ex. 12.56). Now I have still wrapped the input section in a while loop, but now we have the try and except statements in the mix. Here is what I am telling Python to do:

  • Try to
    • Take the the users input
    • Change the users input in to a float
    • Assign that float
  • If any of the things you try raise an error
    • Tell the user they failed
    • Set change back to “none” (so the while loop will execute again)
    • Don’t show the user any errors or stop the program
      • That is what the “pass” keyword does in the except block

This will work for any of Python’s type convert functions.  With this method you get the best of both worlds. You get your input converted and you take care of validation.  If you did this with a regular expression you would have all the over head of the regular expression engine, and you still wouldn’t be 100% sure if Python can convert the type because you haven’t asked it to yet .

Advertisements

1 Comment »

  1. Why not use an IN check instead of an OR?

    print “temp conversion program”
    valid_types = [“ctf”,”ftc”]
    conType = “none”
    while not (conType in valid_types):
    conType = raw_input(“cel to fahr. or fahr to cel (enter ctf or ftc): “)
    sTemp = “none”

    Comment by Anthony Heidenreich — November 2, 2011 @ 12:40 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: