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

I want to convert numbers between 1-12 to months, like this For example

input: 3
            outuput: March, Spring

well something like that. Thank u I've tried some other things but it didn't work either, this code works but halfly

uski9992
  • 21
  • 1
  • 5
  • 1
    hint: make use of a dictionary – Pac0 Nov 17 '20 at 12:24
  • The code seems to do the opposite of what you describe - it takes a month string and converts it to a number... – Tomerikoo Nov 17 '20 at 12:29
  • I couldn't do the thing I wanted and because of that I made it this way, not because I don't wanted or something like that, I just couldn't do it. – uski9992 Nov 17 '20 at 12:32
  • But it's exactly the other way around. Can you be more clear about your problem? I mean, just as you did `if month == 'December': num_month = '12; Season: Winter'` Can't you change it to `if month == 12: num_month = 'December; Season: Winter'`? – Tomerikoo Nov 17 '20 at 12:41
  • My bad if I asked wrong, I'm new on stack, but I think u made a great point i can change it. Thank u – uski9992 Nov 17 '20 at 16:19

2 Answers2

0

Use this as your code:

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

Here is how it should be with the classes. Its not efficient, so you should better use Dictionaries for such program. Here is fixed copy of your code.

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)

Input: 9 Output: September Season: Autumn