So I had to write some code for my class that takes a date input and based on the month/day outputs the season. When I use "in" on the 10th line I don't get any output, not even 'Invalid' the only time I get an output is when is use 'not in'. I'm confused why that happens if anyone could explain it to me I would really appreciate it. Thank you.
input_month = input()
input_day = int(input())
month_winter = ['January', 'February', 'March', 'December']
month_spring = ['March', 'April', 'May', 'June']
month_summer = ['June', 'July', 'August', 'September']
month_autumn = ['September', 'October', 'November', 'December']
month_list = [month_winter, month_spring, month_summer, month_autumn]
def month_check():
if input_month not in month_list[0:]:
return which_month()
def which_month():
if input_month in month_winter[0:]:
which_dates1()
elif input_month in month_summer[0:]:
which_dates2()
elif input_month in month_spring[0:]:
which_dates3()
elif input_month in month_autumn[0:]:
which_dates4()
else:
print('Invalid')
def which_dates1():
if input_month in month_winter[0:2]:
if 0 < input_day < 32:
print('Winter')
else:
print('Invalid')
elif input_month in month_winter[2:3]:
if 1 <= input_day <= 19:
print('Winter')
else:
print('Invalid')
elif input_month in month_winter[3:]:
if 21 <= input_day <= 31:
print('Winter')
else:
print('Invalid')
def which_dates2():
if input_month in month_spring[0:1]:
if 20 <= input_day < 32:
print('Spring')
elif input_month in month_spring[1:3]:
if 1 <= input_day <= 31:
print('Spring')
elif input_month in month_spring[3:]:
if 1 <= input_day <= 20:
print('Spring')
else:
print('Invalid')
def which_dates3():
if input_month in month_summer[0:1]:
if 21 <= input_day < 32:
print('Summer')
elif input_month in month_summer[1:3]:
if 1 <= input_day < 32:
print('Summer')
elif input_month in month_winter[3:]:
if 1 <= input_day <= 21:
print('Summer')
else:
print('Invalid')
def which_dates4():
if input_month in month_autumn[0:1]:
if 22 <= input_day < 32:
print('Autumn')
elif input_month in month_autumn[1:3]:
if 1 <= input_day < 32:
print('Autumn')
elif input_month in month_autumn[3:]:
if 1 <= input_day <= 20:
print('Autumn')
else:
print('Invalid')
month_check()
month-listis a list of lists. I am guessingmonth_list[0:]doesn't do what you think it does (it does nothing useful here - it returns a 'slice' of the original list that contains the whole list). Because it is a list of lists andinput_monthis just a string, theincheck is always false.month_listto be a list of all the month names? We might describe this as "concatenating" the individual season lists. There are several ways to do that in Python, e.g. you can add the lists together:month_list = month_winter + month_spring + month_summer + month_autumn...import itertoolsand thenmonth_list = list(itertools.chain(month_winter, month_spring, month_summer, month_autumn))docs.python.org/3/library/itertools.html#itertools.chainandpart of the same statement you might be able to reach the else statement).