""" days_till.py: Assignment 3, CIS 210, Fall 2012 Author: Harold Hamm Calculates the number of days from a given date (month, day, and year) to the next occurrence of another date (month and day). """ START_MONTH = int(input("Please enter the start month as an integer: ")) START_DAY = int(input("Please enter the day of the start month as an" " integer: ")) START_YEAR = int(input("Please enter the start year as an integer: ")) def check_start_date(): """Determines if the start date is valid. Checks the starting month, day, and year. If the inputs fall outside the accepted ranges, the program prints an error and exits. """ days = days_in_month(START_MONTH, START_YEAR) if (START_MONTH > 12 or START_DAY > days or START_YEAR < 1800): print("Sorry, I need a valid date between 1/1/1800 and 12/31/2200") exit(1) def days_in_month(month, year): """Finds the number of days in a given month. Checks the given month and year, then returns the correct number of days, taking leap years into account. Args: month: The given month. year: The given year. Returns: For April, June, September, or November, it returns 30. For February in a leap year, it returns 29. For February not in a leap year, it returns 28. Otherwise, it returns 31. """ if month == 4 or month == 6 or month == 9 or month == 11: days = 30 elif month == 2: if leap_year(year): days = 29 else: days = 28 else: days = 31 return days def leap_year(year): """Checks for leap years. Checks if the given year is a leap year. Args: year: The given year. Returns: True for years that are evenly divisible by 400 or 4. False for years that are evenly divisible by 100. False for any other year. """ if year % 400 == 0 or year % 4 == 0: return True elif year % 100 == 0: return False else: return False check_start_date() END_MONTH = int(input("Please enter the end month as an integer: ")) END_DAY = int(input("Please enter the day of the end month as an" " integer: ")) def check_end_date(month, day): """Determines if the end date is valid. Checks the ending month and day. Makes an exception for February 29. If the inputs fall outside the accepted ranges, the program prints an error and exits. Args: month: The given ending month. day: The day of the ending month. """ days = days_in_month(month, end_year()) if month == 2 and day == 29: month = 3 day = 1 elif month > 12 or day > days: print("Sorry, that month and year don't look right") exit(1) def end_year(): """Finds the ending year. Calculates if the ending year is the same as the start year or if it is the next year. Returns: The ending year. """ end_year = START_YEAR if END_MONTH <= START_MONTH: end_year = START_YEAR + 1 return end_year check_end_date(END_MONTH, END_DAY) def month_days(): """Calculates the days in each whole month. Calculates the number of days in all the whole months between the starting month and the ending month. Returns: The number of days for all the whole months. """ days = 0 if START_MONTH == END_MONTH: days = 0 current_month = next_month(START_MONTH) while current_month != END_MONTH: if end_year() == START_YEAR: days += days_in_month(current_month, START_YEAR) else: days += days_in_month(current_month, end_year()) current_month = next_month(current_month) return days def next_month(current_month): """Calculates the next month. Takes the current month and gives the next month. Returns: The next month. """ if current_month == 12: next_month = 1 else: next_month = current_month + 1 return next_month def main(): """Calculates and prints the total days to go. Calculates and prints the total number of days between the starting date and the ending date. """ start_month_days = days_in_month(START_MONTH, START_YEAR) - START_DAY if START_MONTH == END_MONTH and END_DAY > START_DAY: total_days = END_DAY - START_DAY else: total_days = start_month_days + month_days() + END_DAY print(total_days, "days to go") main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question