Below is my code to solve this problem.

l = 'Summer' v = 'Spring' o = 'Autumn' z = 'Winter' b = {1 : z, 2 : z, 3 : v, 4 : v, 5 : v, 6 : l, 7 : l, 8 : l, 9 : o, 10 : o, 11 : o, 12 : z} a = int(input()) if a < 1 or a > 12: print('Error') else: print(b[a]) 

How can I set several keys so that there is something like 1, 2, 12 = 'Winter'? And if it is not difficult to offer the shortest ways of your decision, I will be grateful.

    1 answer 1

    I suppose you mean something like this:

     monthsBySeason = { 'winter': [12, 1, 2], 'spring': [3, 4, 5], 'summer': [6, 7, 8], 'autumn': [9, 10, 11] } userInput = int(input()) userOutput = '' for season in monthsBySeason: if userInput in monthsBySeason[season]: userOutput = season.capitalize() break else: userOutput = 'There is no such month' print(userOutput) 

    I also want to draw your attention to the fact that some, especially Europeans, like to consider the onset of a new season differently. In this regard, here is another example .

    You can also take the following array for the season "map":

     [1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1] 

    Here indices will play a special role - zero and the first is Winter, the 2nd / 3rd / 4th is Spring. Etc. Then you can get something like this:

     month = int(input()) seasons = ['Winter', 'Spring', 'Summer', 'Autumn'] monthsBySeason = [1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1] if month < 1 or month > 12: print('There is not such month') else: print(seasons[monthsBySeason[month - 1] - 1])