I need to write a function to build a string based on user input. when user provide one type group of input: "tab" and link variable the string should be tab_link: xxxx. when user provide one type group of input: "subtab" and link variable the string should be subtab_link: subtabxxxxx.
the logic is pretty much like following function. but do I have better way to do it then using if, else? because lets say if I have multiple group, does it mean I need to have more if, elif, elif to identify different group input and match the string?
subtab = {'subtab1': 1, 'subtab2': 2, 'subtab3': 3}
_dir = {'tab': 'tab_link:{}', 'subtab': 'subtab_link:{}'}
def func(**kwargs):
if kwargs['_type'] == 'tab':
print(_dir[kwargs['_type']].format(kwargs['_link']))
else:
print(_dir[kwargs['_type']].format(subtab[kwargs['_link']]))
func(_type='tab', _link='a')
func(_type='tab', _link='b')
func(_type='subtab', _link='subtab1')
func(_type='subtab', _link='subtab2')
#expect
tab_link:a
tab_link:b
subtab_link:1
subtab_link:2