Is there a way to remove too many if else conditions?
<p>I am currently making an interactive system using python, that is able to understand and reply. Hence for this there are lots of conditions for machine to analyze and process. For eg. take the following code(for reference only):</p>

<pre><code>    if ('goodbye') in message:                          
        rand = ['Goodbye Sir', 'Jarvis powering off in 3, 2, 1, 0']
        speekmodule.speek(rand,n,mixer)
        break

    if ('hello') in message or ('hi') in message:
        rand = ['Wellcome to Jarvis virtual intelligence project. At your service sir.']
        speekmodule.speek(rand,n,mixer)

    if ('thanks') in message or ('tanks') in message or ('thank you') in message:
        rand = ['You are wellcome', 'no problem']
        speekmodule.speek(rand,n,mixer)

    if message == ('jarvis'):
        rand = ['Yes Sir?', 'What can I doo for you sir?']
        speekmodule.speek(rand,n,mixer)

    if  ('how are you') in message or ('and you') in message or ('are you okay') in message:
        rand = ['Fine thank you']
        speekmodule.speek(rand,n,mixer)

    if  ('*') in message:
        rand = ['Be polite please']
        speekmodule.speek(rand,n,mixer)

    if ('your name') in message:
        rand = ['My name is Jarvis, at your service sir']
        speekmodule.speek(rand,n,mixer)
</code></pre>

<p>So, is there a way in which I can replace all these if else conditions?? Because there are much more conditions going to be, and it will make the execution slower. </p>

<p>Firstly create a <code>dict</code> object with the key as <code>tuple</code> of string you want to match in your <code>message</code> and associate it with the value <code>string</code> which your Jarvis is suppose to respond. For example:</p>

<pre><code>jarvis_dict = {
    ('goodbye',)  : ['Goodbye Sir', 'Jarvis powering off in 3, 2, 1, 0'],
    ('hello', 
     'hi')        : ['Wellcome to Jarvis virtual intelligence project. At your service sir.'],
    ('thanks', 
     'tanks', 
     'thank you') : ['You are wellcome', 'no problem'],
    ('jarvis',)   : ['Yes Sir?', 'What can I doo for you sir?'],
    ('how are you', 
     'and you', 
     'are you okay'): ['Fine thank you'],
    ('*',)        : ['Be polite please'],
    ('your name',): ['My name is Jarvis, at your service sir']
}
</code></pre>

<p>Now iterate each key of you <code>dict</code> to check whether any sub-string is the part of the message and if there is any match, call the <code>speekmodule.speek(rand,n,mixer)</code> function as:</p>

<pre><code>for key, value in jarvis_dict.items():
    if any(item in message for item in key):
        speekmodule.speek(value, n, mixer)
</code></pre>

<p><em>Note:</em> Here I am assuming that <code>speekmodule.speek(value, n, mixer)</code> in your code is working as there is no information available in your code regarding there declaration. I just replaced your <code>rand</code> with <code>value</code> as it the same list of <code>str</code> returned by the <code>dict</code> which is used in your code.</p>

python