Python - Execute a String Code For Fractions
<p>I've been working in some type of calculator for fractions using <code>from fractions import *</code> following the next logic:</p>

<pre><code>a = Fraction(1,4)
b = Fraction(2,5)
c = Fraction(3,4)

print(a+b*c)
</code></pre>

<p><strong>OUTPUT</strong></p>

<pre><code>11/20
</code></pre>

<p>But I need to execute the statement from a string, just like <code>1/4 + 1/2</code> and for some reason always returns me <strong>0</strong> or <strong>1</strong>:</p>

<pre><code>from fractions import *

class main():
    for i in range(0, 10):
        print "\t\nWRITE YOUR OPERATION"
        code = 'print('
        s = raw_input()
        elements = s.split()

        for element in elements:
            print(element)

            if '/' in element:
                fraction = element.split('/')
                numerator = fraction[0]
                denominator = fraction[1]
                a = Fraction(int(numerator),int(denominator))
                code = code + str(a)
            else:
                code = code + str(element)                

        code = code + ')'        

        exec code
</code></pre>

<p>It's something I'm missing? </p>

<p><strong>EDIT 1</strong></p>

<p>I know what is wrong here</p>

<p>The string code is like this:</p>

<pre><code>code = 'print(1/4+2/5*3/4)'
</code></pre>

<p>But what I really need is this (which I think is imposible to do):</p>

<pre><code>code = 'print(Fraction(1,4)+Fraction(2,5)*Fraction(3,4))'
</code></pre>

<p>Or there is a way to do something like this...?</p>