【问题标题】:How can i convert number between 1-12 to months and give season name also(Pyhon) [duplicate]我如何将 1-12 之间的数字转换为月份并给出季节名称(Pyhon)[重复]
【发布时间】:2021-02-28 15:59:44
【问题描述】:
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

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

【问题讨论】:

  • 提示:使用字典
  • 代码似乎与您描述的相反 - 它需要一个月的字符串并将其转换为数字......
  • 但恰恰相反。你能更清楚你的问题吗?我的意思是,就像你做的那样if month == 'December': num_month = '12; Season: Winter' 你不能把它改成if month == 12: num_month = 'December; Season: Winter'吗?
  • 如果我问错了,我很糟糕,我是堆栈的新手,但我认为你提出了一个很好的观点,我可以改变它。谢谢你

标签: python


【解决方案1】:

这是课程应该如何处理的。它效率不高,因此您最好将字典用于此类程序。这是您的代码的固定副本。

class Monthes:
    def __init__(self,month):
        self.month = month

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

      return num_month

inp = int(input())
obj1 = Monthes(inp)
print(obj1)

输入:9 输出:九月季节:秋天

【讨论】:

    【解决方案2】:

    将此用作您的代码:

    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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 2012-08-07
      相关资源
      最近更新 更多