首页 > 解决方案 > 我如何将 1-12 之间的数字转换为月份并给出季节名称(Pyhon)

问题描述

class Monthes:
    def init(self,month):
        self.month = month.lower()

    def numb_month(self):
      month = self.month

      num_month = ''
      if month == 'December':
          num_month = '12; Season: Winter'
      elif month == 'January':
          num_month = '1;  Season: Winter'
      elif month == 'February':
         num_month = '2;  Season: Winter'
      elif month == 'March':
          num_month = '3 Season: Spring'
      elif month == 'April ':
           num_month = '4 Season: Spring'
      elif month == 'May ':
          num_month = '5 Season: Spring'
      elif month == 'June':
          num_month = '6 Season: Summer'
      elif month == 'July':
         num_month = '7 Season: Summer'
      elif month == 'August':
        num_month = '8 Season: Summer'
      elif month == 'September':
        num_month = '9 Season: Autumn'
      elif month == 'October':
        num_month = '10 Season: Autumn'
      elif month == 'November':
          num_month ='11;  Season: Autumn'
      return num_month


n = input("import month:  ")
p =Monthes(n)
print(p.numb_month())

我想将 1-12 之间的数字转换为月份,例如

input: 3
            outuput: March, Spring

以及类似的东西。谢谢你我已经尝试了一些其他的东西,但它也没有工作,这段代码工作但有一半

标签: python

解决方案


将此用作您的代码:

def numb_month(m):
    sessions = {"1":"January, Winter", "2":"February, Winter", "3":"March, Winter",\
    "4":"April , Sprint", "5":"May , Sprint", "6":"June , Sprint", \
    "7":"July, Summer", "8":"August, Summer", "9":"September, Summer", \
    "10":"October, Autumn", "11":"November, Autumn", "12":"December, Autumn"}

    return (sessions[m])

m = input("import month:  ")
print(numb_month(m))

推荐阅读