Creating a Calc Function that performs arithmetic in Python
<p>Ok, so I'm trying to create a function in Python named calc that can perform the 4 basic arithmetic operations.</p>

<pre><code>def calc(n, num1, num2):

    if n == '+':
        return num1 + num2

    elif n == '-':
        return num1 - num2

    elif n == 'x':
        return num1 * num2

    elif n == '/':
        return num1 / num2
</code></pre>

<p>This is my code so far. So when I execute it like so, I get a syntax error pointing to the number 6, the 3rd argument to be passed.</p>

<pre><code>calc(+ 4 6)

SyntaxError: invalid syntax
</code></pre>

<p>Can someone tell me what's wrong? I'm just now learning python and I'm expected to create an Interpreter with loops, conditions, functions and variable assignments so getting stuck on this makes me somewhat frustrated, any help is appreciated.</p>

<p>You can't make a function which would handle this:</p>

<pre><code>calc(+ 4 6)
</code></pre>

<p>That is invalid syntax.</p>

<p>But you could do this:</p>

<pre><code>calc('+', 4, 6)
</code></pre>

<p>You function would already work with it.</p>

<p>Or you could have this:</p>

<pre><code>calc('+ 4 6')
</code></pre>

<p>and then a function which has to parse the contents.</p>

python function