Changeset 391 for python/trunk/Demo/scripts/unbirthday.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Demo/scripts/unbirthday.py
r2 r391 11 11 12 12 def main(): 13 # Note that the range checks below also check for bad types,14 # e.g. 3.14 or (). However syntactically invalid replies15 # will raise an exception.16 13 if sys.argv[1:]: 17 14 year = int(sys.argv[1]) 18 15 else: 19 16 year = int(raw_input('In which year were you born? ')) 20 if 0 <=year<100:17 if 0 <= year < 100: 21 18 print "I'll assume that by", year, 22 19 year = year + 1900 23 20 print 'you mean', year, 'and not the early Christian era' 24 elif not (1850 <=year<=2002):21 elif not (1850 <= year <= time.localtime()[0]): 25 22 print "It's hard to believe you were born in", year 26 23 return 27 # 24 28 25 if sys.argv[2:]: 29 26 month = int(sys.argv[2]) 30 27 else: 31 28 month = int(raw_input('And in which month? (1-12) ')) 32 if not (1 <=month<=12):29 if not (1 <= month <= 12): 33 30 print 'There is no month numbered', month 34 31 return 35 # 32 36 33 if sys.argv[3:]: 37 34 day = int(sys.argv[3]) … … 42 39 else: 43 40 maxday = calendar.mdays[month] 44 if not (1 <=day<=maxday):41 if not (1 <= day <= maxday): 45 42 print 'There are no', day, 'days in that month!' 46 43 return 47 # 44 48 45 bdaytuple = (year, month, day) 49 46 bdaydate = mkdate(bdaytuple) 50 47 print 'You were born on', format(bdaytuple) 51 # 48 52 49 todaytuple = time.localtime()[:3] 53 50 todaydate = mkdate(todaytuple) 54 51 print 'Today is', format(todaytuple) 55 # 52 56 53 if bdaytuple > todaytuple: 57 54 print 'You are a time traveler. Go back to the future!' 58 55 return 59 # 56 60 57 if bdaytuple == todaytuple: 61 58 print 'You were born today. Have a nice life!' 62 59 return 63 # 60 64 61 days = todaydate - bdaydate 65 62 print 'You have lived', days, 'days' 66 # 63 67 64 age = 0 68 65 for y in range(year, todaytuple[0] + 1): 69 66 if bdaytuple < (y, month, day) <= todaytuple: 70 67 age = age + 1 71 # 68 72 69 print 'You are', age, 'years old' 73 # 70 74 71 if todaytuple[1:] == bdaytuple[1:]: 75 72 print 'Congratulations! Today is your', nth(age), 'birthday' … … 89 86 90 87 def mkdate((year, month, day)): 91 # Januar i1st, in 0 A.D. is arbitrarily defined to be day 1,88 # January 1st, in 0 A.D. is arbitrarily defined to be day 1, 92 89 # even though that day never actually existed and the calendar 93 90 # was different then... 94 days = year*365 # years, roughly91 days = year*365 # years, roughly 95 92 days = days + (year+3)//4 # plus leap years, roughly 96 93 days = days - (year+99)//100 # minus non-leap years every century
Note:
See TracChangeset
for help on using the changeset viewer.