I have a Python program asking the user for input like a shell, and if I detect some specific keywords I want to go inside some specific functions.
The thing is that I would like to avoid doing a lot of if
and else if
. Usually in C to avoid this situation I use a function pointer array that I travel with a while
and use strcmp
to check the input.
I would like to know how to do that in Python if it is even possible.
In Python you use a dictionary.
Example:
keyword2func = {
"word1": function_word1,
"word2": function_word2,
}
word = input("")
keyword2func[word]()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With