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
The dates for each season are:
- Spring: March 20 - June 20
- Summer: June 21 - September 21
- Autumn: September 22 - December 20
- Winter: December 21 - March 19
Here is my code:
input_month = input()
input_day = int(input())
if input_month == "September" and input_day > 21:
print("Autumn")
elif input_month == "September" and input_day <= 21:
print("Summer")
elif input_month == "October":
print("Autumn")
elif input_month == "November":
print("Autumn")
if input_month == "December" and input_day >= 21:
print("Winter")
elif input_month == "December" and input_day <= 20:
print("Autumn")
elif input_month == "January":
print("Winter")
elif input_month == "February":
print("Winter")
if input_month == "March" and input_day >= 20:
print("Spring")
elif input_month == "March" and input_day <= 19:
print("Winter")
elif input_month == "April":
print("Spring")
elif input_month == "May":
print("Spring")
if input_month == "June" and input_day >= 21:
print("Summer")
elif input_month == "June" and input_day <= 20:
print("Spring")
elif input_month == "July":
print("Summer")
elif input_month == "August":
print("Summer")
else:
print("Invalid")
It's giving me a few errors:
1: Compare output
0 / 1
Output differs. See highlights below.
Special character legend
Input
April
11
Your output
Spring
Invalid
Expected output
Spring
2: Compare output
0 / 2
Output differs. See highlights below.
Special character legend
Input
March
3
Your output
Winter
Invalid
Expected output
Winter
4: Compare output
0 / 1
Output differs. See highlights below.
Special character legend
Input
February
39
Your output
Winter
Invalid
Expected output
Invalid
6: Compare output
0 / 1
Output differs. See highlights below.
Special character legend
Input
November
7
Your output
Autumn
Invalid
Expected output
Autumn
7: Compare output
0 / 1
Output differs. See highlights below.
Special character legend
Input
September
31
Your output
Autumn
Invalid
Expected output
Invalid
8: Compare output
0 / 1
Output differs. See highlights below.
Special character legend
Input
December
-1
Your output
Autumn
Invalid
Expected output
Invalid
CodePudding user response:
There are two problems with your code.
First, you're printing Invalid
after many of the correct results because some of your conditions begin with if
instead of elif
. Only the first condition in the entire sequence should use if
, the rest should all be elif
. Because of this, the final else:
block only applies to the sequence of conditions from the last if
; any date that matched a previous condition will also be reported as Invalid
.
Second, you never check for days that are too low or high to be valid days in the month. For example
elif input_month == "October":
should be
elif input_month == "October" and 1 <= input_day <= 31:
CodePudding user response:
That many if statments quickly becomes hard to read. I would suggest using match case to simplify the program. Here is an example
input_month = input("Enter the Month: ")
input_day = int(input("Enter the Day: "))
def monthCase(month, day):
match month:
case "January" as month:
return ("Winter")
case "February" as month:
return ("Winter")
case "March" as month:
if(day < 20):
return ("Winter")
else:
return ("Spring")
case "April" as month:
return ("Spring")
case "May" as month:
return ("Spring")
case "June" as month:
if(day < 21):
return ("Spring")
else:
return ("Summer")
case "July" as month:
return ("Summer")
case "August" as month:
return ("Summer")
case "September" as month:
if(day < 22):
return ("Summer")
else:
return ("Autumn")
case "October" as month:
return ("Autumn")
case "November" as month:
return ("Autumn")
case "December" as month:
if(day < 20):
return ("Autumn")
else:
return ("Winter")
case _:
return "Invalid Month"
print (monthCase(input_month, input_day)) #print the result