Home > OS >  Please help me use the selection description to write a program that displays the corresponding four
Please help me use the selection description to write a program that displays the corresponding four

Time:10-12

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

CodePudding user response:

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.

def season(month: int = 1):
  return ["Winter", "Spring", "Summer", "Autumn"][month % 12 // 3]

Example:

for i in range(1, 13):
  print(i, season(i))

# Outputs
1 Winter
2 Winter
3 Spring
4 Spring
5 Spring
6 Summer
7 Summer
8 Summer
9 Autumn
10 Autumn
11 Autumn
12 Winter
  • Related