Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 273ad45

Browse files
author
Moshe Zadka
committed
Updated to use new Python features
Reindented
1 parent 8b6f989 commit 273ad45

1 file changed

Lines changed: 86 additions & 86 deletions

File tree

Demo/scripts/unbirthday.py

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,97 +10,97 @@
1010
import calendar
1111

1212
def main():
13-
# Note that the range checks below also check for bad types,
14-
# e.g. 3.14 or (). However syntactically invalid replies
15-
# will raise an exception.
16-
if sys.argv[1:]:
17-
year = eval(sys.argv[1])
18-
else:
19-
year = input('In which year were you born? ')
20-
if year in range(100):
21-
print 'I\'ll assume that by', year,
22-
year = year + 1900
23-
print 'you mean', year, 'and not the early Christian era'
24-
elif year not in range(1850, 2000):
25-
print 'It\'s hard to believe you were born in', year
26-
return
27-
#
28-
if sys.argv[2:]:
29-
month = eval(sys.argv[2])
30-
else:
31-
month = input('And in which month? (1-12) ')
32-
if month not in range(1, 13):
33-
print 'There is no month numbered', month
34-
return
35-
#
36-
if sys.argv[3:]:
37-
day = eval(sys.argv[3])
38-
else:
39-
day = input('And on what day of that month? (1-31) ')
40-
if month == 2 and calendar.isleap(year):
41-
maxday = 29
42-
else:
43-
maxday = calendar.mdays[month]
44-
if day not in range(1, maxday+1):
45-
print 'There are no', day, 'days in that month!'
46-
return
47-
#
48-
bdaytuple = (year, month, day)
49-
bdaydate = mkdate(bdaytuple)
50-
print 'You were born on', format(bdaytuple)
51-
#
52-
todaytuple = time.localtime(time.time())[:3]
53-
todaydate = mkdate(todaytuple)
54-
print 'Today is', format(todaytuple)
55-
#
56-
if bdaytuple > todaytuple:
57-
print 'You are a time traveler. Go back to the future!'
58-
return
59-
#
60-
if bdaytuple == todaytuple:
61-
print 'You were born today. Have a nice life!'
62-
return
63-
#
64-
days = todaydate - bdaydate
65-
print 'You have lived', days, 'days'
66-
#
67-
age = 0
68-
for y in range(year, todaytuple[0] + 1):
69-
if bdaytuple < (y, month, day) <= todaytuple:
70-
age = age + 1
71-
#
72-
print 'You are', age, 'years old'
73-
#
74-
if todaytuple[1:] == bdaytuple[1:]:
75-
print 'Congratulations! Today is your', nth(age), 'birthday'
76-
print 'Yesterday was your',
77-
else:
78-
print 'Today is your',
79-
print nth(days - age), 'unbirthday'
13+
# Note that the range checks below also check for bad types,
14+
# e.g. 3.14 or (). However syntactically invalid replies
15+
# will raise an exception.
16+
if sys.argv[1:]:
17+
year = int(sys.argv[1])
18+
else:
19+
year = int(raw_input('In which year were you born? '))
20+
if 0<=year<100:
21+
print "I'll assume that by", year,
22+
year = year + 1900
23+
print 'you mean', year, 'and not the early Christian era'
24+
elif not (1850<=year<=2002):
25+
print "It's hard to believe you were born in", year
26+
return
27+
#
28+
if sys.argv[2:]:
29+
month = int(sys.argv[2])
30+
else:
31+
month = int(raw_input('And in which month? (1-12) '))
32+
if not (1<=month<=12):
33+
print 'There is no month numbered', month
34+
return
35+
#
36+
if sys.argv[3:]:
37+
day = int(sys.argv[3])
38+
else:
39+
day = int(raw_input('And on what day of that month? (1-31) '))
40+
if month == 2 and calendar.isleap(year):
41+
maxday = 29
42+
else:
43+
maxday = calendar.mdays[month]
44+
if not (1<=day<=maxday):
45+
print 'There are no', day, 'days in that month!'
46+
return
47+
#
48+
bdaytuple = (year, month, day)
49+
bdaydate = mkdate(bdaytuple)
50+
print 'You were born on', format(bdaytuple)
51+
#
52+
todaytuple = time.localtime()[:3]
53+
todaydate = mkdate(todaytuple)
54+
print 'Today is', format(todaytuple)
55+
#
56+
if bdaytuple > todaytuple:
57+
print 'You are a time traveler. Go back to the future!'
58+
return
59+
#
60+
if bdaytuple == todaytuple:
61+
print 'You were born today. Have a nice life!'
62+
return
63+
#
64+
days = todaydate - bdaydate
65+
print 'You have lived', days, 'days'
66+
#
67+
age = 0
68+
for y in range(year, todaytuple[0] + 1):
69+
if bdaytuple < (y, month, day) <= todaytuple:
70+
age = age + 1
71+
#
72+
print 'You are', age, 'years old'
73+
#
74+
if todaytuple[1:] == bdaytuple[1:]:
75+
print 'Congratulations! Today is your', nth(age), 'birthday'
76+
print 'Yesterday was your',
77+
else:
78+
print 'Today is your',
79+
print nth(days - age), 'unbirthday'
8080

8181
def format((year, month, day)):
82-
return '%d %s %d' % (day, calendar.month_name[month], year)
82+
return '%d %s %d' % (day, calendar.month_name[month], year)
8383

8484
def nth(n):
85-
if n == 1: return '1st'
86-
if n == 2: return '2nd'
87-
if n == 3: return '3rd'
88-
return '%dth' % n
85+
if n == 1: return '1st'
86+
if n == 2: return '2nd'
87+
if n == 3: return '3rd'
88+
return '%dth' % n
8989

9090
def mkdate((year, month, day)):
91-
# Januari 1st, in 0 A.D. is arbitrarily defined to be day 1,
92-
# even though that day never actually existed and the calendar
93-
# was different then...
94-
days = year*365 # years, roughly
95-
days = days + (year+3)/4 # plus leap years, roughly
96-
days = days - (year+99)/100 # minus non-leap years every century
97-
days = days + (year+399)/400 # plus leap years every 4 centirues
98-
for i in range(1, month):
99-
if i == 2 and calendar.isleap(year):
100-
days = days + 29
101-
else:
102-
days = days + calendar.mdays[i]
103-
days = days + day
104-
return days
91+
# Januari 1st, in 0 A.D. is arbitrarily defined to be day 1,
92+
# even though that day never actually existed and the calendar
93+
# was different then...
94+
days = year*365 # years, roughly
95+
days = days + (year+3)/4 # plus leap years, roughly
96+
days = days - (year+99)/100 # minus non-leap years every century
97+
days = days + (year+399)/400 # plus leap years every 4 centirues
98+
for i in range(1, month):
99+
if i == 2 and calendar.isleap(year):
100+
days = days + 29
101+
else:
102+
days = days + calendar.mdays[i]
103+
days = days + day
104+
return days
105105

106106
main()

0 commit comments

Comments
 (0)