0

Can I use this method in coding on python 3 ?

def switch_demo(argument):

    switcher = {
        51>switcher>13: "January",
        2: "February",
        3: "March",
        4: "April",
        5: "May",
        6: "June",
        7: "July",
        8: "August",
        9: "September",
        10: "October",
        11: "November",
        90>switcher>52: "December"
    }

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

My question is this:

I want to get range of number and return code.

quamrana
  • 37,849
  • 12
  • 53
  • 71

2 Answers2

1

Perhaps you are talking about two different things: Some out of range values and then the in range values of 1-12:

def switch_demo(argument):
    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"
    }
    return switcher.get(argument, "Invalid month")

def switch_range(argument):
    if argument <= 12:
        return switch_demo(argument)
    elif argument <= 51:
        return switch_demo(1)
    else:
        return switch_demo(12)
quamrana
  • 37,849
  • 12
  • 53
  • 71
1

Got answer from Range as dictionary key in Python

Try this if you want a range of values as keys for a dictionary

switcher = dict(
                [(n, 'January')
                    for n in range(13,51)] +
                [(2, 'February')] +
                [(3, 'March')] +
                [(4, 'April')] +
                [(5, 'May')] +
                [(6, 'June')] +
                [(7, 'July')] +
                [(8, 'August')] +
                [(9, 'September')] +
                [(10, 'October')] +
                [(11, 'November')] +
                [(n, 'December') 
                    for n in range(52,90)]
                )
Will
  • 11
  • 3