Get the season when entering a month and day (python)

Question:

I am writing a code that allows the user to enter a month and date and the program will tell them what season it will be given the information that the user entered. What I currently have working is that when you enter certain months that have the season in all their days it will display the season. But when I enter a month and date that has 2 seasons it gives me an error. This is the season guide that I am following:

Spring: March 20 - June 20
Summer: June 21 - September 21
Autumn: September 22 - December 20
Winter: December 21 - March 19

What I am trying to figure out is why the program is not printing out stuff when certain dates are entered. For example if I enter March 25th it should print out spring but it doesnt.

This is my code:

input_month = input('Enter a month: ')
input_day = input('Enter a date: ')

month_lower = input_month.lower()

seasons = ()

if month_lower == 'january' or month_lower == 'febuary' or month_lower == 'march' and input_day <19:
    seasons = 'winter'

elif month_lower == 'march' and input_day <=19 or month_lower == 'april' or month_lower == 'may' or month_lower == 'june' and input_day < 21:
    seasons = 'spring'

elif month_lower == 'july' or month_lower == 'august' or month_lower == 'september' and input_day <22:
    seasons = 'summer'

elif month_lower == 'october' or month_lower == 'november' or month_lower == 'december' and input_day <20:
    seasons = 'autumn'
else:
    print('Please enter a valid date')

print(seasons)
Asked By: iconstar

||

Answers:

I supposed you’re getting input not an int error when entering date since you compare input_day to an int, quick fix for this is to change all input_day to int(input_day).
But if your problem is that march 25 not printing out what you want but instead gave out please enter valid date then that’d be because you’ve not gave it a condition just yet, all above condition are average less than 20 but none talk about 22-30

Answered By: Vuong Duong

You have a problem in the spring condition, you should check if the day is strictly superior to 19:

elif month_lower == 'march' and input_day > 19 or month_lower == 'april' or month_lower == 'may' or month_lower == 'june' and input_day < 21:
    seasons = 'spring'
Answered By: César Debeunne