1

I am writing a program that takes input from the user for a Month and Day, and then determines which of the four seasons the day belongs to. We were given the specific seasons below, and the user is to input a Month and day.

Below is what I came up with. It likely can be simplified... but, I'm still wondering why no output is produced when executing this in PyCharm.. when it is run in another IDE it outputs 'Invalid'.

Sample input:

March
25

I have checked the user input to make sure it is matching against the months, and also that the day is less than 31. (likely not the best solution). I am checking to see that the month is in range of the season, and then checking the date is in range of the season. To take care of the months that wouldn't meet the day requirement, I check that the month is greater than and less than the start and end month for the season, this way day can be ignored for those months that are in between. Else print Invalid.

Please advise on how to resolve this.

from collections import namedtuple
season = namedtuple('Season', ['start_mo', 'start_mo_int', 'start_day', 'end_mo', 'end_mo_int', 'end_day'])
spring = season('March', 3, 20, 'June', 6, 20)
summer = season('June', 6, 21, 'September', 9, 21)
autumn = season('September', 9, 22, 'December', 12, 20)
winter = season('December', 12, 21, 'March', 3, 19)
months = {
    'January': 1,
    'February': 2,
    'March': 3,
    'April': 4,
    'May': 5,
    'June': 6,
    'July': 7,
    'August': 8,
    'September': 9,
    'October': 10,
    'November': 11,
    'December': 12
}
input_month = input()
input_day = int(input())
im = months.get(input_month, -1)
if input_month in months and (input_day <= 31):
    if (((im >= spring.start_mo_int) and (im <= spring.end_mo_int)) 
            and ((input_day >= spring.start_day) and (input_day <= spring.end_day))) \
        or ((months.get(input_month, -1) > spring.start_mo_int) 
            and (months.get(input_month) < spring.end_mo_int)):
        print('Spring')
    elif (((im >= summer.start_mo_int) and (im <= summer.end_mo_int)) 
          and ((input_day >= summer.start_day) and (input_day <= summer.end_day))) \
        or ((months.get(input_month, -1) > summer.start_mo_int) 
            and (months.get(input_month) < summer.end_mo_int)):
        print('Summer')
    elif (((im >= autumn.start_mo_int) and (im <= autumn.end_mo_int)) 
          and ((input_day >= autumn.start_day) and (input_day <= autumn.end_day))) \
        or ((months.get(input_month, -1) > autumn.start_mo_int) 
            and (months.get(input_month) < autumn.end_mo_int)):
        print('Autumn')
    elif (((im >= winter.start_mo_int) and (im <= winter.end_mo_int)) 
          and ((input_day >= winter.start_day) and (input_day <= winter.end_day))) \
        or ((months.get(input_month, -1) > winter.start_mo_int) 
            and (months.get(input_month) < winter.end_mo_int)):
        print('Winter')
else:
    print('Invalid')
Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
Casey
  • 11
  • 1

0 Answers0