'Writing a program that takes a date as input and outputs the date's season or invalid
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: Blue 65 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
Here is my question, everything works fine except month that end on the 30th (April, June, September and November). Right now if I input June 31 the output should be 'Invaild' but it is not showing any output. I put my code in below, any help would greatly be appreciated.
month = input('Enter month: ')
day = int(input('Enter day: '))
months = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
]
if month not in months or (day <= 0) or (day > 31):
print('Invalid')
Spring
elif (month == 'March' and day >= 20 or month == 'April' and day <= 30 or \
month == 'May' and day or month == 'June' and day <= 20):
if (day <= 0) or (day > 31):
print('Invalid')
else:
print('Spring')
Summer
elif (month == 'June' and day >= 21 and day <= 30 or month == 'July'and day or \
month == 'August' and day or month == 'September' and day <= 21):
if (day <= 0) or (day > 31):
print('Invalid')
else:
print('Summer')
Autumn
elif (month == 'September' and day >= 22 and day <= 30 or \
month == 'October' and day or month == 'November' and day <= 30 or \
month == 'December' and day <= 20):
if (day <= 0) or (day > 31):
print('Invalid')
else:
print('Autumn')
Winter
elif (month == 'December' and day >= 21 and day <= 31 or \
month == 'January' and day or month == 'February' and day or \
month == 'March' and day <= 19 and day >= 31):
if (day <= 0) and (day > 31):
print('Invalid')
else:
print('Winter')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
