Calculating Year, Month and Day of Life

Bengt Richter bokr at
Sun Jul 6 00:26:47 CEST 2003

On Sat, 05 Jul 2003 15:51:58 -0400, hokiegal99 <hokiegal99 at> 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.:

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:
                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.

Bengt Richter

More information about the Python-list mailing list