In python
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
Then 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:

The program in Python is as follows:

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

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

month = input("Month: ")

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

   print("Invalid")

else:

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

   if month_index == 0:

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

       else:            print("Winter")

   elif month_index == 1:

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

       else:            print("Winter")

   elif month_index == 2:

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

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

       else:            print("Spring")

   elif month_index == 3:

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

       else:            print("Spring")

   elif month_index == 4:

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

       else:            print("Spring")

   elif month_index == 5:

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

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

       else:            print("Summer")

   elif month_index == 6:

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

       else:            print("Summer")

   elif month_index == 7:

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

       else:            print("Summer")

   elif month_index == 8:

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

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

       else:            print("Autumn")

   elif month_index == 9:

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

       else:            print("Autumn")

   elif month_index == 10:

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

       else:            print("Autumn")

   elif month_index == 11:

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

       elif days<21:            print("Autumn")

       else:            print("Winter")

Explanation:

See attachment for complete source file, where comments are used to explain difficult lines

Ver imagen MrRoyal