Feb-24-2017, 08:38 PM
In python we switch on dictionaries.
A brief (and silly) example:
A brief (and silly) example:
def func_one(): print("Hey from func one.") def func_two(): print("Hey from func two.") def default(): print("I'm some default func.") SWITCH_DICT = {".3" : func_one, ".4" : func_two} user_input = input("Enter .3 or .4: ") SWITCH_DICT.get(user_input, default)()
Output:Enter .3 or .4: .3
Hey from func one.
Output:Enter .3 or .4: .4
Hey from func two.
Output:Enter .3 or .4: spam
I'm some default func.
This technique is extremely common and useful in a wide variety of situations.