Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is April 11, the output is: spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is invalid, the output is: invalid The dates for each season are: spring: March 20 - June 20 summer: June 21 - September 21 autumn: September 22 - December 20 winter: December 21 - March 19

Respuesta :

Answer:

#SECTION 1

while True:

   month = input("Input the month (e.g. January, February etc.): ")

   try:

       

       if month in ('January', 'February', 'March','April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December',):

           day = int(input("Input the day: "))

           try:

               if day > 31:

                   raise

               elif day == 31 and month in ('September', 'April', 'June', 'November'):

                   raise

               elif day > 29 and month == 'February':

                   raise

               else:

                   break

                   

           except:

               print('Invalid!!!')

               print('Day not correct')

           

       else:

           raise

   except:

       print('Invalid!!!')

       print('Enter correct Month')

 

     

#SECTION 2

if month in ('January', 'February', 'March'):

season = 'winter'

elif month in ('April', 'May', 'June'):

season = 'spring'

elif month in ('July', 'August', 'September'):

season = 'summer'

else:

season = 'autumn'

if (month == 'March') and (day > 19):

season = 'spring'

elif (month == 'June') and (day > 20):

season = 'summer'

elif (month == 'September') and (day > 21):

season = 'autumn'

elif (month == 'December') and (day > 20):

season = 'winter'

print("Season is",season)

Explanation:

#SECTION 1

This section ensures that a correct input is inserted. The try and except blocks in combination with IF statements are used to archive the result. The while loop will continue to run until a valid input is inserted.

In the first try block, It checks to see if the month inputted is a month that exists by comparing it  with a list of months, if it does not exist it raises an error and executes the except block and prints invalid and states the error that occurred.

If the first  TRY block executes successful, the program moves to the next, it takes an input for the number and ensures that us a valid number for each month. If the number is invalid, it raises an error and executes the try block which prints invalid and states the problem.

If al inputs are valid the program breaks out of the loop and proceeds to the next section.

#SECTION 2

In this section the data provided is used to calculate the season, by making use of IF statements and finally the season is printed.

I have attached a sample for you to see how the code runs by inputting some inaccurate values and accurate ones.

Ver imagen jehonor

The program is an illustration of conditional statements.

Conditional statements are statements whose execution is dependent on its truth value.

The program in Python where comments are used to explain each line is as follows:

#This initializes a list of months

mnths = ["january","february","march","april","may","june","july","august","september","october","november","december"]

#This gets input for days

days = int(input("Day: "))

#This gets input for months

month = input("Month: ")

#This checks if the input month is present in the list of months.

if not(month.lower() in mnths):

   #Print invalid, if not

   print("Invalid")

#If yes,

else:

   #This reads the month index, from the list where 0 - January...... 11 - December

   month_index = mnths.index(month.lower())

   #For January

   if month_index == 0:

       #If days is not between 1 and 31 (inclusive), then the date is invalid

       if days<1 or days>31:            print("Invalid")

       #else, all days in January are winter

       else:            print("Winter")

   #For February

   elif month_index == 1:

       #If days is not between 1 and 28 (inclusive), then the date is invalid

       if days<1 or days>28:            print("Invalid")

       #else, all days in February are winter

       else:            print("Winter")

   #For March

   elif month_index == 2:

       #If days is not between 1 and 31 (inclusive), then the date is invalid

       if days<1 or days>31:            print("Invalid")

       #Days less than 20 are Winter

       elif days<= 19:            print("Winter")

       #Others are spring

       else:            print("Spring")

   #For April

   elif month_index == 3:

       #If days is not between 1 and 30 (inclusive), then the date is invalid

       if days<1 or days>30:            print("Invalid")

       #else, all days in April are winter

       else:            print("Spring")

   #For May

   elif month_index == 4:

       #If days is not between 1 and 31 (inclusive), then the date is invalid

       if days<1 or days>31:            print("Invalid")

       #else, all days in May are winter

       else:            print("Spring")

   #For June

   elif month_index == 5:

       #If days is not between 1 and 30 (inclusive), then the date is invalid

       if days<1 or days>30:            print("Invalid")

       #Days less than 21 are Spring

       elif days<= 20:            print("Spring")

       #Others are summer

       else:            print("Summer")

   #For July

   elif month_index == 6:

       #If days is not between 1 and 31 (inclusive), then the date is invalid

       if days<1 or days>31:            print("Invalid")

       #else, all days in July are summer

       else:            print("Summer")

   #For August

   elif month_index == 7:

       #If days is not between 1 and 31 (inclusive), then the date is invalid

       if days<1 or days>31:            print("Invalid")

       #else, all day in August are Summer

       else:            print("Summer")

   #For September

   elif month_index == 8:

       #If days is not between 1 and 31 (inclusive), then the date is invalid

       if days<1 or days>31:            print("Invalid")

       #Days less than 22 are Summer

       elif days<= 21:            print("Summer")

       #Others are Autumn

       else:            print("Autumn")

   #For October

   elif month_index == 9:

       #If days is not between 1 and 31 (inclusive), then the date is invalid

       if days<1 or days>31:            print("Invalid")

       #All days in October are Autumn

       else:            print("Autumn")

   #For November

   elif month_index == 10:

       #If days is not between 1 and 30 (inclusive), then the date is invalid

       if days<1 or days>30:            print("Invalid")

       #Else, all days are Autumn

       else:            print("Autumn")

   #For December

   elif month_index == 11:

       #If days is not between 1 and 31 (inclusive), then the date is invalid

       if days<1 or days>31:            print("Invalid")

       #Days less than 21 are Autumn

       elif days<21:            print("Autumn")

       #Others are Winter

       else:            print("Winter")

Read more about similar programs at:

https://brainly.com/question/24077371