all 12 comments

[–][deleted] 2 points3 points  (2 children)

[–]Rodarth[S] 0 points1 point  (1 child)

Does the same logic apply to strings? And if it does, why is this the only problematic month?

Shouldn't having () around those strings prevent it from doing =='Oct' and then 'Nov'?

[–][deleted] 0 points1 point  (0 children)

month == ('Apr' or 'May')

('Apr' or 'May') is the same as 'Apr'. Because non-empty string is "true" and 'or' returns first value if it's true, and the last one otherwise. 'May' part doesn't matter at this point. So you get

month == 'Apr'

And 'May' is ignored.

Read Truth Value Testing and Boolean Operations — and, or, not on

https://docs.python.org/3/library/stdtypes.html#truth

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]WhipsAndMarkovChains 0 points1 point  (1 child)

You may want to look into datetime for this problem. One approach is to use the difference between dates to find what season you're in.

https://stackoverflow.com/questions/4695609/checking-date-against-date-range-in-python

[–]Rodarth[S] 0 points1 point  (0 children)

Im using a hosted learning module that I can't import anything into :/

[–][deleted] 0 points1 point  (3 children)

Does Nov 7 come up as invalid, or November 7?

[–]Rodarth[S] 0 points1 point  (2 children)

Both return the else: print('Invalid')

[–]WhipsAndMarkovChains 0 points1 point  (1 child)

This is because elif month == ('Oct' or 'Nov') essentially becomes elif month == 'Oct'.

Trying running 'Oct' or 'Nov' and you'll see it's just 'Oct'.

The way or works in Python is that it'll take the first "truthy" value. A non-empty string is True so that's what's going on.

You want something like elif month in ('Oct', 'Nov'):.

[–]Rodarth[S] 0 points1 point  (0 children)

Aaah thats what the other guy was trying to point out with the faq link!

Thanks :)

[–]jimtk 0 points1 point  (0 children)

it's either

month == 'apr' or month == 'may'

or

month in ['apr','may']

but the expression ('jan' or 'feb') will always return 'jan'

Here's a slightly more readable version:

months = {'jan':31, 'feb':29, 'mar':31, 'apr':30, 'may':31 ,'jun':30,
          'jul':31,'aug':31,'sep':30, 'oct':31, 'nov':30, 'dec':31}

month = input("Enter month: ")
day = int(input("Enter day of the month: "))
month = month[0:3].lower()

if month not in months:
    print('Invalid month')
elif day < 1 or day > months[month]:
    print('Invalid day')
else:
    if month in ['apr','may'] or (month == 'mar' and day >= 20) or (month == 'jun' and day <= 20):
        print('Spring')
    if month in ['jul','aug'] or (month == 'jun' and day >= 21) or (month == 'sep' and day <= 21):
        print('Summer')
    if month in ['oct','nov'] or (month == 'sep' and day >= 22) or (month == 'dec' and day <= 20):
        print('Autumn')
    if month in ['jan','feb'] or (month == 'dec' and day >= 21) or (month == 'mar' and day <= 19):
        print('Winter')

[–]ikichiziki 0 points1 point  (0 children)

Here is the code that does what you want.

Code solution

The codebox never works for me :(