2

I've been working in some type of calculator for fractions using from fractions import * following the next logic:

a = Fraction(1,4)
b = Fraction(2,5)
c = Fraction(3,4)

print(a+b*c)

OUTPUT

11/20

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

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

It's something I'm missing?

EDIT 1

I know what is wrong here

The string code is like this:

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

But what I really need is this (which I think is imposible to do):

code = 'print(Fraction(1,4)+Fraction(2,5)*Fraction(3,4))'

Or there is a way to do something like this...?

3 Answers3

2

You can use ast eval

import ast

def myfunc():
   localvars = {}
   expr = 'x = 1/4 + 1/2'
   eval(compile(ast.parse(expr), '<input>', mode="exec"), localvars)
   return localvars['x']

Note, in python 2 it will yield 0 as both 1/4 and 1/2 will result in 0, you need to do 1.0/4 in python 2. python3 will calculate it using double types so it will return 0.75 as you expect , ie python2 version:

 import ast

 def myfunc():
    localvars = {}
    expr = 'x = 1.0/4 + 1.0/2'
    eval(compile(ast.parse(expr), '<input>', mode="exec"), localvars)
    return localvars['x']
vittore
  • 17,449
  • 6
  • 44
  • 82
2

I tried this and this is working (my input was in the form a/b + c/d):

from fractions import*

print ("your operation:")

op = input()
#print (op)

elements = op.split()

for el in elements:
    if ('/' in el):
       fraction = el.split('/')
       numerator = fraction[0]
       denominator = fraction[1]

       a = Fraction(int(numerator),int(denominator))

    print (numerator)
    print (denominator)
    print (a)
Jay T.
  • 307
  • 1
  • 6
1

You can do something like this:

from fractions import *

def returnElement(element):
    if '/' in element and len(element) > 1:
        fraction = element.split('/')
        numerator = fraction[0]
        denominator = fraction[1]
        return 'Fraction(' + numerator + ',' + denominator + ')'        
    else:
        return element  

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:
            code = code + returnElement(element)           

        code = code + ')'        

        print code
        exec code
CyborgNinja23
  • 290
  • 12
  • 33