0

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()

5
  • 1
    month-list is a list of lists. I am guessing month_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 and input_month is just a string, the in check is always false. Commented Oct 20, 2022 at 19:41
  • Did you want month_list to 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 ... Commented Oct 20, 2022 at 19:43
  • ...another way would be to import itertools and then month_list = list(itertools.chain(month_winter, month_spring, month_summer, month_autumn)) docs.python.org/3/library/itertools.html#itertools.chain Commented Oct 20, 2022 at 19:45
  • The problem with the which_dates function that you are having with invalid not printing is that one of the if statements are triggering before that (if you included the other if statement with an and part of the same statement you might be able to reach the else statement). Commented Oct 20, 2022 at 19:46
  • @Anentropic I tried concatenating the way you showed with the add operator but it still doesn't give any output whenever I use the 'in' operator; however importing itertools did work and passed 9 / 10 tests. The way I had it before passed all 10, so I'll go back and see why it's failing the last one. Thank you. Commented Oct 20, 2022 at 20:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

ahh that makes sense, that explains why it gave me an output when I passed one of the list's names as the input. Thank you.
Sure no problem. Always keep track of different list by logging them (this has particularly helped me :) ). Also if my answer solves your problem you can tick (accept) my answer and close this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.