py_get_age.py

#!/usr/bin/python

import time

class BadUserError(Exception):
    pass

def get_integer(retrieve,question,attempts=3):
    """
    Most code supplied by Jervis Whitley
    
    A small function to attempt to retrieve information 
    from a user, given a prompt question.

    retrive - any function that will accept a string as
    an argument and return a string or other response
    from the user.

    question - a string type question that the user
    inputs an answer

    attempts[optional] - number of times the user can
    incorrectly enter data befor the BadUserError is raised.
    """
    
    while attempts > 0:
        num = retrieve(question)
        try: # check if the user input is an integer
            return int(num)
        except ValueError:
            print "Opps, You must enter a number!"
        attempts -= 1
    raise BadUserError("Too many incorrect attempts!")

curr_date = time.strftime("%Y %m %d", time.gmtime())
print "Please enter the date format as: ", curr_date

yr = get_integer(raw_input, "\nWhat year were you born? ")
mn = get_integer(raw_input, "What month were you born? ")
dy = get_integer(raw_input, "What day were you born? ")

today = time.gmtime()

ynum = today.tm_year - yr
mnum = today.tm_mon
dnum = today.tm_mday

if mn < mnum:
    print "You are", ynum, "years old."
elif mn == mnum and dy <= dnum:
    print "You are", ynum, "years old."
else:
    print "You are", ynum - 1, "years old."