Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day.
Ex: If the input is:
April 11 the output is:
Spring In addition, check if the string and int are valid (an actual month and day).
Ex: If the input is:
Blue 65 the output is:
Invalid
input_month = input()
input_day = int(input())
if input_month == ('March'):
if 20 <= input_day <= 31:
print('Spring')
if input_month == ('April' or 'May'):
if 1 <= input_day <= 31:
print('Spring')
if input_month == ('June'):
if 0 <= input_day <= 20:
print('Spring')
if input_month == ('June'):
if 21 <= input_day <= 30:
print('Summer')
if input_month == ('July' or 'August'):
if 1 <= input_day <= 31:
print('Summer')
if input_month == ('September'):
if 0 <= input_day <= 21:
print('Summer')
if input_month == ('September'):
if 22 <= input_day <= 30:
print('Autumn')
if input_month == ('October' or 'November'):
if 1 <= input_day <= 31:
print('Autumn')
if input_month == ('December'):
if 0 <= input_day <= 20:
print('Autumn')
if input_month == ('December'):
if 21 <= input_day <= 31:
print('Winter')
if input_month == ('January' or 'February'):
if 1 <= input_day <= 31:
print('Winter')
if input_month == ('March'):
if 0 <= input_day <= 19:
print('Winter')
else:
input_month != 'January' or input_month != 'February' or input_month != 'March' or input_month != 'April' or input_month != 'May' or input_month != 'June' or input_month != 'July' or input_month != 'August' or input_month != 'September' or input_month != 'October' or input_month != 'November' or input_month != 'December'
print('Invalid')
if 0 >= input_day >= 32:
print('Invalid')
I keep getting the output as
Spring
Invalid
when it should just be the season
CodePudding user response:
Correct syntax to combine two conditions using logical or
operator.
if x == "something" or x == "another thing":
Or you can use in
operator instead
if x in [str1,str2]:
CodePudding user response:
There are several ways you could organize this. I think the simplest would be to group the conditions based on the output they need to produce. In other words, the line print("Spring")
should only appear once:
if ((input_month == "March" and 20 <= input_day <= 31)
or (input_month == "April" and 1 <= input_day <= 30)
or (input_month == "May" and 1 <= input_day <= 31)
or (input_month == "June" and 1 <= input_day <= 20)):
print("Spring")
elif ((input_month == "June" and 21 <= input_day <= 30)
or (input_month == "July" and 1 <= input_day <= 31)
or (input_month == "August" and 1 <= input_day <= 31)
or (input_month == "September" and 1 <= input_day <= 21)):
print("Summer")
# more elifs here
else:
print("Invalid")