1
input_month = input()
input_day = int(input())
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

    
if (input_month in 'March') and (20 < input_day <= 31):
    print ('Spring')
if (input_month in 'April') and (1 <= input_day <= 30): 
    print ('Spring')
if (input_month in 'May') and (1 <= input_day <= 31):
    print ('Spring')
if (input_month in 'June') and (1 <= input_day <= 20):
    print ('Spring')
    
if (input_month in 'June') and (20 < input_day <= 30):
    print ('Summer')
if (input_month in 'July') and (1 <= input_day <= 31):
    print ('Summer')
if (input_month in 'August') and (1 <= input_day <= 31):
    print ('Summer')
if (input_month in 'September') and (1 <= input_day < 21):
    print ('Summer')
    
if (input_month in 'September') and (21 <= input_day <= 30):
    print ('Autumn')
if (input_month in 'October') and (1 <= input_day <= 31):
    print ('Autumn')
if (input_month in 'November') and (1 <= input_day <= 30):
    print ('Autumn')
if (input_month in 'December') and (1 <= input_day < 21):
    print ('Autumn')  
    
if (input_month in 'December') and (21 <= input_day <= 31):
    print ('Winter')
if (input_month in 'January') and (1 <= input_day <= 31):
    print ('Winter')
if (input_month in 'February') and (1 <= input_day <= 29):
    print ('Winter')
if (input_month in 'March') and (1 <= input_day < 20):
    print ('Winter')
    
else: 
    print('Invalid')

For most months and day it works, but I am getting: April 11 'Spring' (which is correct) but also getting 'Invalid' and not sure why I am getting both June 21 'Summer' (which is correct) but also getting 'Invalid' and not sure why I am getting both November 7 'Autumn' (which is correct) but also getting 'Invalid' and not sure why I am getting both

I just don't understand why this is... could someone please help me understand my errors. Thank you

3
  • 1
    The else is only attached to your last if. Commented Jul 18, 2021 at 1:23
  • First of all, replace in with ==. Commented Jul 18, 2021 at 1:52
  • Worth nothing, but in might be somewhat appropriate because it supports abbreviations, albeit not case sensitive. Maybe startswith is better. Commented Jul 18, 2021 at 2:24

3 Answers 3

1

else is only attached to the last if-elif chain that precedes it. But since you didn't use elif the final else is only attached to the last if and will fire whenever the condition in that if is false regardless of the outcome of all other if conditions in your code.

There are many easier ways to do this rather than error-prone enumeration of each and every bound.

For example, you could use a dictionary keyed by month which returns a function that accepts day as its only argument and uses the value of day to compute the season:

seasons = {
    "January": lambda day: "Winter",
    "February": lambda day: "Winter",
    "March": lambda day: "Winter" if day < 21 else "Spring",
    "April": lambda day: "Spring",
    "May": lambda day: "Spring",
    "June": lambda day: "Spring" if day < 21 else "Summer",
    "July": lambda day: "Summer",
    "August": lambda day: "Summer",
    "September": lambda day: "Summer" if day < 22 else "Autumn",
    "October": lambda day: "Autumn",
    "November": lambda day: "Autumn",
    "December": lambda day: "Autumn" if day < 22 else "Winter",
}

Given this data structure, you can use it with seasons["March"](20).

Since you need to validate, I'd make that a distinct step. You can keep another dictionary that maps month names to integers representing their end date or number of days. Validation is then a matter of ensuring the key exists and that the day is >= 1 and <= days_by_month[some_month]. For example,

def validate(month, day):
    days_by_month = {
        "January": 31,
        "February": 28, # FIXME handle leap years
        "March": 31,
        # ... etc ...
    }
    return month in days_by_month and 1 <= day <= days_by_month[month]

validate("January", 31) # => True

Since February fluctuates, you may want to add an extra condition that uses the year to determine whether it's a leap year.

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

1 Comment

Thank you this was very helpful... I am just starting python but really enjoy it. 3 weeks in but this is something I never thought of
0

Use elif instead of if. Like this:

a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

Comments

-1

The else statement at the end is only attached to the last if. You would want to use elif for the majority of the conditions:

input_month = input()
input_day = int(input())
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

    
if (input_month in 'March') and (20 < input_day <= 31):
    print ('Spring')
elif (input_month in 'April') and (1 <= input_day <= 30): 
    print ('Spring')
elif (input_month in 'May') and (1 <= input_day <= 31):
    print ('Spring')
elif (input_month in 'June') and (1 <= input_day <= 20):
    print ('Spring')
    
elif (input_month in 'June') and (20 < input_day <= 30):
    print ('Summer')
elif (input_month in 'July') and (1 <= input_day <= 31):
    print ('Summer')
elif (input_month in 'August') and (1 <= input_day <= 31):
    print ('Summer')
elif (input_month in 'September') and (1 <= input_day < 21):
    print ('Summer')
    
elif (input_month in 'September') and (21 <= input_day <= 30):
    print ('Autumn')
elif (input_month in 'October') and (1 <= input_day <= 31):
    print ('Autumn')
elif (input_month in 'November') and (1 <= input_day <= 30):
    print ('Autumn')
elif (input_month in 'December') and (1 <= input_day < 21):
    print ('Autumn')  
    
elif (input_month in 'December') and (21 <= input_day <= 31):
    print ('Winter')
elif (input_month in 'January') and (1 <= input_day <= 31):
    print ('Winter')
elif (input_month in 'February') and (1 <= input_day <= 29):
    print ('Winter')
elif (input_month in 'March') and (1 <= input_day < 20):
    print ('Winter')
    
else: 
    print('Invalid')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.