[Tutor] Exception Handling

David david at abbottdavid.com
Mon Dec 29 02:42:45 CET 2008


David wrote:
> Hi,
> Is this the correct way to handle a ValueError exception and should I 
> get in the practice of catching them? Also any suggestions on the program.
> thanks
> -david
> 
Might help if I included the program :)

#!/usr/bin/python
import time

print "Enter year as 0000"
print "Enter month and day as 00"

while True:
     try:
         yr = int(raw_input("What year were you born? "))
         mn = int(raw_input("What month were you born? "))
         dy = int(raw_input("What day were you born? "))
         curr_date = time.strftime("%Y %m %d", time.gmtime())

         ynum = int(time.strftime("%Y", time.gmtime())) - int(yr)
         mnum = int(time.strftime("%m", time.gmtime()))
         dnum = int(time.strftime("%d", time.gmtime()))
         mn = int(mn)
         dy = int(dy)

         if mn - mnum:
             print "You are %i" % ynum, "years old."
             break
         elif mn == mnum and dy < dnum:
             print "You are %i" % ynum, "years old."
             break
         else:
             ret = int(ynum) - 1
             print "You are %i" % ret, "years old."
             break
     except ValueError:
         print "Oops, You must enter a number!"



-- 
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
pgp.mit.edu



More information about the Tutor mailing list