[Tutor] Exception Handling

Jervis Whitley jervisau at gmail.com
Wed Dec 31 01:37:52 CET 2008


On Wed, Dec 31, 2008 at 11:18 AM, David <david at abbottdavid.com> wrote:

>
> . I still need to get it to prduce an error if the year is 0 or 2009, the
> month is 0 or 13 and the day is 0 or 32.


Try using the datetime module to check validity of entered data.

example:
>>> import datetime

>>> datetime.datetime(2008, 12, 32) # should be an error, there are only 31
days in December!
ValueError: day is out of range for month




> david [06:56 PM] opteron ~ $ ./py_get_age.py
> Please enter the date format as:  2008 12 30
>
> What year were you born? 2010
> What month were you born? 14
> What day were you born? 34
> You are -3 years old.


You can also use it to check if the input is in the future.

>>> userdate = datetime.datetime(2010, 1, 1)
>>> datetime.datetime.now() > userdate # a check to see if userdate is in
the past.
False

Cheers,

Jervis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081231/973587cb/attachment.htm>


More information about the Tutor mailing list