-2

I am newbie in python i want to a input from user and print month name i dont know what i am doing wrong in this code correct me

value=raw_input("Enter value to print month ")
    def switch_demo(argument=value):
        switcher = {
            1: "January",
            2: "February",
            3: "March",
            4: "April",
            5: "May",
            6: "June",
            7: "July",
            8: "August",
            9: "September",
            10: "October",
            11: "November",
            12: "December"
        }
        print switcher.get(argument, "Invalid month")

    switch_demo(value)
Vijay
  • 197
  • 10

2 Answers2

0

raw_input accepts value and return it as a string. So when you enter 1 or 2, it is returned as '1' or '2'.

You have to typecast the input value into int explicitly:

value=int(raw_input("Enter value to print month "))

Or,you can use input() to accept user input as:

value = input("Enter value to print month ")
Ganesh Negi
  • 1,939
  • 1
  • 11
  • 9
-1

Your problem is that your "switcher" dictionary has integer keys, and argument is a string when you call

print switcher.get(argument, "Invalid month")

One possible solution is to cast argument to int, it should then work.

print switcher.get(int(argument), "Invalid month")
NonoxX
  • 134
  • 10