In [6]:
days_in_month = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
days_in_month_leap = {1:31,2:29,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
In [7]:
# for regular year
# accumulator pattern
days_in_previous_months = {}
sum = 0 # accumulator variable
for i in range(1,13):
    days_in_previous_months[i] = sum 
    sum = sum + days_in_month[i]
print(days_in_previous_months)
{1: 0, 2: 31, 3: 59, 4: 90, 5: 120, 6: 151, 7: 181, 8: 212, 9: 243, 10: 273, 11: 304, 12: 334}
In [8]:
# for leap year
# accumulator pattern
days_in_previous_months_leap = {}
sum = 0 # accumulator variable
for i in range(1,13):
    days_in_previous_months_leap[i] = sum 
    sum = sum + days_in_month_leap[i]
print(days_in_previous_months_leap)
{1: 0, 2: 31, 3: 60, 4: 91, 5: 121, 6: 152, 7: 182, 8: 213, 9: 244, 10: 274, 11: 305, 12: 335}
In [15]:
ds = input("Enter date in format MM/DD/YYYY: ")
d = ds.split('/')
month = int(d[0])
day = int(d[1])
year = int(d[2])
In [17]:
if month == 1:
    ndays = day
else:
    if year%4 == 0 and (year%100 !=0 or year%400 == 0):
        ndays = day + days_in_previous_months_leap[month]
    else:
        ndays = day + days_in_previous_months[month]
print("The day number for " + ds + " is " + str(ndays))
The day number for 07/13/2021 is 194
In [14]:
print(d[2])
2021
In [18]:
"hello " + "johnny"
Out[18]:
'hello johnny'
In [ ]: