Write a program that prompts the user to enter the year and the first three letters of a month name (with the first letter in uppercase) and displays the number of days in the month.
Sample Run 1
Enter a year: 2001
Enter a month: Jan
Jan 2001 has 31 days
Sample Run 2
Enter a year: 2000
Enter a month: Feb
Feb 2000 has 29 days
Sample Run 3
Enter a month: 2001
Enter a month: jan
jan is not a correct month name In python.

Respuesta :

Answer:

The program in python is as follows

Because of the length of the program, it'll be better to use comments to replace the explanation section; So, read through the comments too

See Attachment from source file (as jpg)

#Initialize list of Valid Months

validmonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]

#Initialize number of days in each months

days = [30,28,31,30,31,30,31,31,30,31,30,31]

check = 0 #Initialize a variable to check if month is valid is or not

year = int(input("Enter a year: ")) #Prompt user for year

month = input("Enter a month: ") #Prompt user for month

for i in range(12): #Iterate through list of valid months to check if user input is valid or not

     if month == validmonths[i]:

           check = i+1

#Check if the declared variable is still 0; If yes, then month is invalid

if check == 0:

     print(month+" is not a correct month")

#Otherwise month is valid

else:

     if not(month == "Feb"): #If month is not Feb, print corresponding number of days

           print(month + " " +str(year) +" has "+str(days[check-1])+" days")

     else: #Otherwise, if month is Feb, then there's a need to check for leap year using the following if statements

           if year%4 == 0:

                 if year%100 == 0:

                       if year%400 == 0:

                             print(month + " " + str(year)+ " has 29 days")

                       else:

                             print(month + " " +  str(year)+ " has 28 days")

                 else:

                       print(month + " " +  str(year)+ " has 29 days")

           else:

                 print(month + " " +  str(year)+ " has 28 days")

Explanation:

Ver imagen MrRoyal
ACCESS MORE