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()
CodePudding user response:
You are trying to find the input_month in a 2 dimensional list i.e. month_list
If you check:
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]
print(month_list)
This will output:
[['January', 'February', 'March', 'December'], ['March', 'April', 'May', 'June'], ['June', 'July', 'August', 'September'], ['September', 'October', 'November', 'December']]
So as you can see all the values are list and not a string as you are expecting in line 10 so in
operator wont work.