Getting an error trying to check month for season

Viewed 34

I'm trying to complete an assignment, but i'm getting stuck. My first if not, elif, else: works for the first month (January). When I try to write the code for the next month (February) i'm getting this error

  File "main.py", line 14
    elif input_month == ('February') and input_day >1 and input_day <29:
    ^
SyntaxError: invalid syntax

Here's what I have so far.

input_month = input()
input_day = int(input())

months = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
days = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31)

if not input_month in months and input_day not in days:
    print('Invalid')
    
elif input_month == ('January') and input_day >=1 and input_day <=31:
    print('Winter')
else:
    print('Invalid')
    
elif input_month == ('February') and input_day >1 and input_day <29:
    print('Winter')
else:
    print('Invalid')
3 Answers

You cannot have an elif after an else and will have to restructure your code to have one else at the end. (Think through the logic - you only need to check that it is not any of the above conditions once, and you need to do it last.)

Without knowing the intent of this program, I can't really fill in a complete solution. However, I know why the error is occurring: the way you've written the conditionals.

You either have to remove the first "else" statement or change the "elif" that follows into an "if" or whatever is appropriate to your needs. That first "else" closes the block of if-elif-else conditionals, so that the "elif" after it gives a syntax error, since what would normally need to begin a new if-elif-else would be "if".

if not input_month in months and input_day not in days:
    print('Invalid')
    
elif input_month == ('January') and input_day >=1 and input_day <=31:
    print('Winter')
    
elif input_month == ('February') and input_day >1 and input_day <29:
    print('Winter')
else:
    print('Invalid')

or

if not input_month in months and input_day not in days:
    print('Invalid')
    
if input_month == ('January') and input_day >=1 and input_day <=31:
    print('Winter')
else:
    print('Invalid')
    
if input_month == ('February') and input_day >1 and input_day <29:
    print('Winter')
else:
    print('Invalid')

You will have to consider the logic of your program and adjust accordingly.

This is cause by your else, which terminates the ongoing if statement and therefore the elif after causes an error. My solution is less reliant on multiple elifs

if  input_month not in months and input_day not in days:
   print("Invalid")
elif input_month ==  "January" and input_day in range(32):
   print("Winter")
elif input_month ==  "January" and input_day not in range(32):
   print("Invalid")
elif input_month == "February" and input_day in range(30):
   print("Winter")
elif input_month == "February" and input_day not in range(30):
   print("Invalid")    

Furthermore, I would like to point out that the design of the code isn't great and could be improved upon to write fewer conditionals. For example, you could construct a dictionary called winter with dictionary comprehension, and take a key value pair input from the user, checking if the input is in the dictionary called winter. Lastly, the code throws an error if the user enters anything other than an integer for a date. This could be improved upon as well, by creating a re-input, if the input is not an integer

Related