SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.selections
Exercise:
Programming Excercises
Question:11 | ISBN:978013274719 | Edition: 6

Question

(Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2000, the program should display that February 2000 has 29 days. If the user entered month 3 and year 2005, the program should display that March 2005 has 31 days.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#Prompt the user to give input
number_Of_DaysInMonth = 0
MonthOfName = "Unknown"
print("Input a month number: ")
month = int(input())
print("Input a year: ")
year = int(input())
#Initialize dictionary
switcher={1:"January",
          2:"February",
          3: "March",
          4:"April",
          5:"May",
          6:"June",
          7:"July",
          8: "August",
          9:"September",
          10: "October",
          11:"November",
          12:"December"}
#Display Results
if(switcher.get(month)=="January"):
    print("January {} has 31 days".format(year))
elif(switcher.get(month)=="February"):
    if((year % 400 == 0) or ((year % 4 == 0) and (year % 100!=0))):
        print("Febrauary {} has 29 days".format(year))
    else:
        print("February {} has 28 days ".format(year))
elif(switcher.get(month)=="March"):
    print("March {} has 31 days".format(year))
elif(switcher.get(month)=="April"):
    print("April {} has 30 days".format(year))
elif(switcher.get(month)=="May"):
    print("May {} has 31 days".format(year))
elif(switcher.get(month)=="June"):
    print("June {} has 30 days".format(year))
elif(switcher.get(month)=="July"):
    print("July {} has 31 days".format(year))
elif(switcher.get(month)=="August"):
    print("August {} has 31 days".format(year))
elif(switcher.get(month)=="September"):
    print("september {} has 30 days".format(year))
elif(switcher.get(month)=="October"):
    print("October {} has 31 days".format(year))
elif(switcher.get(month)=="November"):
    print("November {} has 30 days".format(year))    
elif(switcher.get(month)=="December"):
    print("December {} has 31 days".format(year)) 

Output:

Input a month number: 
12
Input a year: 
7889
December 7889 has 31 days

0 0

Discussions

Post the discussion to improve the above solution.