Answer to Question #172380 in Python for Luke Price

Question #172380

I don't really understand this

Day:

Month: 
Year: 

Then, your program should 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.
Please enter a date
Day: 5
Month: 5
Year: 1984
Menu:
1) Calculate the number of days in the given month.
2) Calculate the number of days left in the given year.
1
31

Sample Output 1

Please enter a date
Menu:
1) Calculate the number of days in the given month.
2) Calculate the number of days left in the given year.
31

Sample Run 2

Please enter a date
Day: 21
Month: 6
Year: 2016
Menu:
1) Calculate the number of days in the given month.
2) Calculate the number of days left in the given year.
2
193

Sample Output 2

Please enter a date
Menu:
1) Calculate the number of days in the given month.
2) Calculate the number of days left in the given year.
193















1
Expert's answer
2021-03-17T04:39:02-0400
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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS