-1

Check this below code:

var = input("enter a number between 1 and 12")
def switch_demo(var):
    switcher = {
                1: "Jan" 
                2: "Feb" 
                3: "March"
                4: "April" 
                5: "May" 
                6: "June" 
                7: "July" 
                8: "August" 
                9: "Sept" 
                10: "Oct" 
                11: "Nov" 
                12: "Dec"
    }
    print switcher.get(var,"Invalid Month")

I am getting Syntax Error at line 5 How can I solve the error?

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
Mahesh Dasari
  • 41
  • 1
  • 2
  • 4

2 Answers2

7

Fixing your Bugs

You need to add a comma at the end of each item:

 1: "Jan", 
 2: "Feb",

Working program:

def switch_demo(var):
    switcher = {
                1: "Jan", 
                2: "Feb", 
                3: "March",
                4: "April", 
                5: "May", 
                6: "June", 
                7: "July", 
                8: "August", 
                9: "Sept", 
                10: "Oct", 
                11: "Nov", 
                12: "Dec"
    }

    return switcher.get(var,"Invalid Month")

var = int(input("enter a number between 1 and 12"))
print(switch_demo(var))

Simpler Solution

You should have a look at the calendar module. It already provides all months names:

>>> import calendar
>>> calendar.month_name[3]
'March' 
Nishant
  • 20,354
  • 18
  • 69
  • 101
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
-2

Switch case is a very powerful control tool in programming as we can control to execute different blocks of code with it. In python you can implement it by using dictionary method and for your code posted,

var = input("enter a number between 1 and 12")
def switch_demo(var):
    switcher = {
                1: "Jan",
                2: "Feb",
                3: "March",
                4: "April",
                5: "May",
                6: "June", 
                7: "July",
                8: "August", 
                9: "Sept",
                10: "Oct",
                11: "Nov",
                12: "Dec"
    }
    print switcher.get(var,"Invalid Month")
NightOwl19
  • 419
  • 5
  • 24