This python code is not solving equations
<p>I have a chat bot for an Instant messenger and I am trying to make a math solver for it but it can't send the solution of equation to the Instant messenger, it sends equation instead.</p>

<p>If someone from Instant messenger sends "solve: 2+2", this program should send them "4" not "2+2".</p>

<p><strong>Main problem:</strong></p>

<pre><code>if (parser.getPayload().lower()[:6]=="solve:"):
                    parser.sendGroupMessage(parser.getTargetID(), str(parser.getPayload()[7:]))
</code></pre>

<p><strong>output:</strong></p>

<p><a href="http://i.stack.imgur.com/Rf4ND.png" rel="nofollow">it's sending same input again not the answer of equation</a></p>

<p><strong>Test:</strong>
I tested something and that's working properly. If I add this code, program will send solution of equation:</p>

<pre><code>if (parser.getPayload().lower()=="test"):
                    parser.sendGroupMessage(parser.getTargetID(), str(2 + 2 -3 + 8 * 7))
</code></pre>

<p><strong>Output:</strong>
<a href="http://i.stack.imgur.com/uHg55.png" rel="nofollow">Working perfectly</a></p>

<p>Your test code</p>

<pre><code>str(2 + 2 -3 + 8 * 7)
</code></pre>

<p>is distinct from your production code</p>

<pre><code>str(parser.getPayload()[7:])
</code></pre>

<p>which gets expanded into</p>

<pre><code>str("2 + 2 -3 + 8 * 7")
</code></pre>

<p>assuming you pass in the same equotation. Good thing is you have the plumbing working, now you need to implement the actual math solver like</p>

<pre><code>str(solve_math(parser.getPayload()[7:]))

def solve_math(expr : str) -&gt; float:
    """
    Parses and evaluates math expression `expr` and returns its result.
    """
</code></pre>

<p>Here you need to first parse the expression string into some structure representing the data, the operators / functions and the evaluation order. So your "2 + 2" expression gets turned into something like <code>Addition(Const(2), Const(2))</code> while expression "2 + 2 * 3" gets turned into somethng like <code>Addition(Const(2), Multiplication(Const(2), Const(3)))</code> and then you need to just evaluate it which should be fairly simple.</p>

<p>I recommend <code>pyparsing</code> to help you with that.</p>

python bots irc instant-messaging payload