I want to select from my function numbers_to_month
and return the appropriate string (e.g. January). What is the best way to do this?
I tried assigning a variable to numbers_to_month
. e.g.
>>> test = numbers_to_months(1)
one
Hoever when I call that variable, i was expecting the appropriate month but i get an error. e.g.
>>> test()
Traceback (most recent call last):
File "<pyshell#96>", line 1, in <module>
test()
TypeError: 'NoneType' object is not callable
My code:
def one():
return "January"
def two():
return "February"
def three():
return "March"
def four():
return "April"
def five():
return "May"
def six():
return "June"
def seven():
return "July"
def eight():
return "August"
def nine():
return "September"
def ten():
return "October"
def eleven():
return "November"
def twelve():
return "December"
def numbers_to_months(argument):
switcher = {
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
10: "ten",
11: "eleven",
12: "twelve"
}
# Get the function from the switcher dictionary
func = switcher.get(argument, lambda: "Invalid Month")
# Execute the function
print(func)