How to take input as operation
<p>I'm trying to make a calculator but little bit different.
I'm thinking if a get input as operation like:</p>

<pre><code>first_number = input('Type first number: ')
first_operation = input('Type your operation (+, -, *, /): ')
second_number = input('Type second number: ')
first_answer = input('Do you wanna stop here y/n: ')
if first_answer = "y":
   print(int(first_number), str(first_operation), int(second_number))
</code></pre>

<p>At the moment, the program needs to make a problem with user's input. For example, if user types <code>3</code> for first number, <code>+</code> for first operation, <code>2</code> for second number. I can only write those inputs like <code>3 + 2</code>, but I want to solve after print answer.</p>

<p>Grab the operations from the <a href="https://docs.python.org/3/library/operator.html" rel="nofollow"><code>operator</code></a> module and create a dictionary of the available operators:</p>

<pre><code>&gt;&gt;&gt; from operator import add, sub, mul, truediv
&gt;&gt;&gt; ops = {'+': add, '-': sub, '*': mul, '/': truediv}
</code></pre>

<p>After doing this you can use <code>first_operation</code> as the <code>key</code> on <code>ops</code>, get the corresponding function for the operation and supply <code>first_number</code> and <code>second_number</code> as its arguments:</p>

<pre><code>&gt;&gt;&gt; first_number = int(input('Type first number: '))
&gt;&gt;&gt; second_number = int(input('Type second number: '))
&gt;&gt;&gt; first_operation = input('Type your operation (+, -, *, /): ')
</code></pre>

<p>Let's say we enter <code>5</code> and <code>10</code> and <code>+</code>, then:</p>

<pre><code>&gt;&gt;&gt; print(ops[first_operation](first_number, second_number))
15
</code></pre>

<p>Note, the numbers are wrapped in <code>int</code> before supplying them to the operator since they behave differently when of type <code>str</code>.</p>

<p>As a simple alternative, you could use <a href="https://docs.python.org/3/library/ast.html#ast.literal_eval" rel="nofollow"><code>literal_eval</code></a> from the <a href="https://docs.python.org/3/library/ast.html#module-ast" rel="nofollow"><code>ast</code></a> module, supplying it an expression as input:</p>

<pre><code>&gt;&gt;&gt; from ast import literal_eval
&gt;&gt;&gt; first_number = input('Type first number: ')
&gt;&gt;&gt; second_number = input('Type second number: ')
&gt;&gt;&gt; first_operation = input('Type your operation (+, -, *, /): ')
</code></pre>

<p>Here you don't need to change anything to <code>int</code>, you just join all arguments and make a string of the overall expression and feed it to <code>literal_eval</code> for it to do its thing.</p>

<pre><code>&gt;&gt;&gt; literal_eval(" ".join([first_number, first_operation, second_number]))
</code></pre>

<p>In essence your call will look like:</p>

<pre><code>&gt;&gt;&gt; literal_eval('5 + 10')
</code></pre>

<p>With a result of 15, again.</p>

python python-3.x