Answer to Question #85622 in Python for john cocoa

Question #85622
Create a calendar program that allows the user to enter a day, month and year in three separate variables. Then ask the user to select from a menu of choices using this formatting:

Please enter a date
Day:
Month:
Year:

Menu:
1) Calculate the number of days in the given month.
2) Calculate the number of days left in the given year.

It must include the following functions:
leap_year: Takes the year as a parameter and returns a 1 if a year is a leap year (Links to an external site.)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 the date as parameters and return how many days are in the given month (Links to an external site.)Links to an external site..

days_left: This will accept the date as parameters and then calculate the number of days left in the year. This should not include the date the user entered in the count.
1
Expert's answer
2019-03-01T01:52:08-0500
import datetime
# function takes the year as a parameter and returns a 1 if a year is a leap year and 0 if it is not 
def leap_year(year):
    if year % 4 == 0:
        if year % 100 == 0 and year % 400 != 0:
            return 0
        else:
            return 1
    else:
        return 0
# function accept the date as parameters and return how many days are in the given month
def number_of_days(date):
    if date.month in [1, 3, 5, 7, 8, 10, 12]:
        return 31
    elif date.month in [4, 6, 9, 11]:
        return 30
    elif date.month == 2:
        if leap_year(date.year) == 1:
            return 29
        else:
            return 28
# function accept the date as parameters and calculate the number of days left in the year
def days_left(date):
    mon = date.month
    days = 0
    while mon <= 12:
        if mon == date.month:
            days = days + number_of_days(datetime.date(date.year, mon, 1)) - date.day
        else:
            days = days + number_of_days(datetime.date(date.year, mon, 1))
        # next month
        mon += 1
    return days
# ask the user to enter a day, month and year
print('Please enter a date')
day = int(input('Day:'))
mon = int(input('Month:'))
year = int(input('Year:'))
# create data variable
date = datetime.date(year, mon, day)
choise = '0'
while choise != '3':
    while True:
        # display menu    
        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.')
        print('3) Exit.')
        # get the user choise
        choise = input('>')
        if choise == '1':
            print('Number of days in the given month is', number_of_days(date))
        elif choise == '2':
            print('Number of days left in the given year is', days_left(date))
        elif choise == '3':
            break;
        else:
            print('Invalid input')

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

Assignment Expert
25.03.20, 21:38

Dear visitor, please use panel for submitting new questions

Jeffrey Brown
24.03.20, 19:20

How do you do it without importing date.time?

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS