Please use the selection description to write a program that displays the corresponding four seasons according to the month entered by the user input Output: (one) Month 11 winter
Advertisement
Answer
Just convert the 12 for December to a 0, perform a floored division by 3 to get a number in the interval [0, 3], then use that number as an index for the different seasons.
You can replace “Autumn” with “Fall” depending on your dialect.
JavaScript
x
3
1
def season(month: int = 1):
2
return ["Winter", "Spring", "Summer", "Autumn"][month % 12 // 3]
3
Example:
JavaScript
1
17
17
1
for i in range(1, 13):
2
print(i, season(i))
3
4
# Outputs
5
1 Winter
6
2 Winter
7
3 Spring
8
4 Spring
9
5 Spring
10
6 Summer
11
7 Summer
12
8 Summer
13
9 Autumn
14
10 Autumn
15
11 Autumn
16
12 Winter
17