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')
CodePudding user response:
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.)
CodePudding user response:
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.