Write a program which accepts as input the date and outputs

Write a program, which accepts as input the date and outputs the date one-week in the future. Assume that the date is represented as three integers In the form mm dd yyyy, where mm is the month, dd th day in month and yyyy the year. A year is a leap year If the following conditions hold; the year is divisible by 4 and not divisible by 100, or the year is divisible by 400.

Solution

#Taking input month, date and year
m = input(\"Input the month: \")
d = input(\"Input the day: \")
y = input(\"Input the year: \")


#function to check if a given year is leap year
def is_leap(y):
    if (y%4 == 0 and y%100 != 0) or y%400==0: #if y is divisible by 4 and not by 100 or if y is divisible by 400, return true
        return True
    else:
        return False #else return false

#function to calculate the day after 7 days
def future_date(m, d, y):

    #if leap year, then the number of days in feb = 29 other wise number of days in feb = 28
    if is_leap(y):
        days_in_month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    else:
        days_in_month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    #setting days allowed depending on the month. For example for october, days allowed = 31
    days_allowed = days_in_month[m]

    #if days overflowing to next month
    if d + 7 > days_allowed:
        day = 7 - (days_allowed - d) #setting day in the next month which is 7 ahead of input date
        if m+1 > 12: #if month overflow, going to next year
            year += 1
            month = 1
        else:
            month = m+1 #else incrementing month
            year = y
    else: #else just increment day by 7 and keep month and year to be same
        day = d+7
        month = m
        year = y

    return month, day, year #returns month, day and year after 7 days

month, day, year = future_date(m, d, y)

print str(month).zfill(2), \'-\', str(day).zfill(2), \'-\', str(year).zfill(4)

 Write a program, which accepts as input the date and outputs the date one-week in the future. Assume that the date is represented as three integers In the form

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site