Answer to Question #106351 in Python for Devik Figueroa

Question #106351
Create a calendar program that allows the user to enter a day, month, and year in three separate variables as shown below.

Please enter a date
Day:
Month:
Year:
Then, ask the user to select from a menu of choices using this formatting:

Menu:
1) Calculate the number of days in the given month.
2) Calculate the number of days left in the given year.
The program must include the following functions:

leap_year: Takes the year as a parameter and returns 1 if a year is a leap year (Links to an external site.) and 0 if it is not. This information will only be used by other functions.
number_of_days: This subprogram will accept two parameters in the following order: month and year. It will return how many days are in the given month (Links to an external site.).
days_left:
1
Expert's answer
2020-03-26T03:41:19-0400
#!/usr/bin/env python3

import sys
import calendar
import datetime

def leap_year(year):
    if calendar.isleap(year):
        return 1
    else:
        return 0

def number_of_days(month, year):
    _, num_of_days = calendar.monthrange(year, month)
    return num_of_days

def days_left(year, month, day):
    user_date = datetime.date(year, month, day)
    end_of_year = datetime.date(year, 12, 31)
    delta = end_of_year - user_date
    return delta.days

if __name__ == "__main__":
    print("Please enter a date")
    print("Day:")
    try:
        day = int(input())
    except ValueError:
        print("Should be digit")
        sys.exit()
    if day > 31 or day < 1:
        print("Wrong day.")
        sys.exit()
    print("Month:")
    try:
        month = int(input())
    except ValueError:
        print("Should be digit")
        sys.exit()
    if month > 12 or month < 1:
        print("Wrong month.")
        sys.exit()
    print("Year:")
    try:
        year = int(input())
    except ValueError:
        print("Should be digit")
        sys.exit()
    print("Menu:")
    print("1) Calculate the number of days in the given month.")
    print("2) Calculate the number of days left in the given year.")
    try:
        choice = int(input())
    except ValueError:
        print("Should be digit")
        sys.exit()
    if choice < 1 or choice > 2:
        print("Wrong choice.")
        sys.exit()
    if choice == 1:
        print(number_of_days(month, year))
    elif choice == 2:
        print(days_left(year, month, day))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS