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.