-1

Have to write a program that when you input the month & day, it will output the correct season.

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

If it doesn't match anything above, to print "Invalid"

input_month = input()
input_day = input()


if input_month in ('April', 'May'):
    print('spring')
elif input_month in ('July', 'August'):
    print('summer')
elif input_month in ('October', 'November'):
    print('autumn')
elif input_month in ('January', 'February'):
    print("winter")


elif input_month == 'June' and (input_day == range(1, 20)):
    print("spring")
elif input_month == 'June' and (input_day == range(21, 30)):
    print("summer")
elif input_month == 'September' and (input_day == range(1, 21)):
    print("summer")
elif input_month == 'September' and (input_day == range(22, 30)):
    print("autumn")
elif input_month == 'December' and (input_day == range(1, 20)):
    print("autumn")
elif input_month == 'December' and (input_day == range(21, 31)):
    print("winter")
elif input_month == 'March' and (input_day == range(1, 19)):
    print("winter")
elif input_month == 'March' and (input_day == range(20, 31)):
    print("spring")

else:
    print("invalid")

If I input anything from the lines without the "and", it works perfect. I've tried parentheses, changing the order of the statements. Anything that includes the "and" statement prints "Invalid"

I have tried changing the '==' for the second input of the and statements to an 'in', still invalid output. I've tried changing some of the elif statements to if, and still invalid.

biggreek19
  • 43
  • 1
  • 1
  • 7
  • 1
    `input_day` type is `str`, so none of your comparisons with ranges pass and you get `invalid` as result. You need to cast `input_day` to integer. – kaveh Aug 27 '19 at 00:50
  • 1
    You don't use `==` to test if something is in a range. Use `int(input_day) in range(1, 20)` – Barmar Aug 27 '19 at 00:53
  • Possible duplicate question https://stackoverflow.com/questions/16139306/determine-season-given-timestamp-in-python-using-datetime – Yann Masoch Aug 27 '19 at 00:59
  • Be aware June has 30 days and your range is [21,30) which won't catch June 30th :) Same for all other ranges. Python ranges and slices are generally inclusive "[" at start and ")" exclusive at end – CJC Aug 27 '19 at 01:20

1 Answers1

1
elif input_month == 'June' and (input_day == range(1, 20)):

I see two problems here:

  1. Comparing a value to a range will always be false -- the value is in the range, not equal to it.

  2. input_day is a string; the range only includes numbers, so the value will never be in the range anyway.

What you want is:

elif input_month == 'June' and int(input_day) in range(1, 20):

Another simpler way of representing this would be … and int(input_day) <= 20.

  • I could not thank you enough for your help. It works flawlessly now. Thank you so much. I had the "input_day = int(input())", and it didn't work the same... any idea why? – biggreek19 Aug 27 '19 at 01:07