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 is designed to ensure that the user inputs a valid month and day. The try and except blocks paired with IF statements work together to achieve accurate results. The while loop continues until a legitimate input is received.
The first try block checks if the month provided exists by comparing it against a predefined list. If it fails, an error is raised, triggering the except block that informs the user of the invalid entry.
When the first TRY block succeeds, it moves to the next stage, which takes a number input, confirming that it is correct for each month. Any invalid input leads to an error, stimulating the try block to print the issue.
If all inputs are valid, the program exits the loop and transitions to the subsequent section.
#SECTION 2
This part uses the given data to determine the season, leveraging IF statements to produce the result, which it then prints.
I have attached a sample for you to see how the code operates using both incorrect and correct input values.