Answer to Question #278949 in Python for Vivek Patel

Question #278949

Create a calendar program that allows you to enter a day , month, year and in three separate variables



1
Expert's answer
2021-12-13T00:02:22-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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS