Calculating Year, Month and Day of Life

Bengt Richter bokr at oz.net
Sat Jul 5 18:26:47 EDT 2003


On Sat, 05 Jul 2003 15:51:58 -0400, hokiegal99 <hokiegal99 at hotmail.com> wrote:

>hokiegal99 wrote:
>
>> hokiegal99 wrote:
>> 
[...]
>
>Once again, I'm making progress on my own. Here's where I stand now:
>
>----------------------
>from time import *
>local = localtime()
>
>y = input("Enter the year of birth: ")
>m = input("Enter the month of birth: ")
>d = input("Enter the day of birth: ")
>	
>age = y,m,d
>print "You are", local[0] - age[0], "years", local[1] - age[1], "months"
>-----------------------
>
Looks like you're enjoying yourself, so I won't interfere too much ;-)

You might want to look at the calendar module that comes with python
if you want to get age as exact total number of days or day of week on birth date, etc.:

    http://www.python.org/doc/current/lib/module-calendar.html

Also, you might want to consider providing for re-try in a function,
and using raw_input instead of input(), e.g.,

    def getymd(prompt):
        while 1:
            try:
                return int(raw_input(prompt))
            except ValueError, e:
                print '    Error:', e

    y = getinp("Enter the year of birth: ")
    m = getinp("Enter the month of birth: ")
    d = getinp("Enter the day of birth: ")

Of course, you could validate y,m,d integer values and retry the date entry as a whole too.

Regards,
Bengt Richter




More information about the Python-list mailing list