0

I'm having a problem which I had for ages without finding a solution.

The code is this:

    Df = range(100)
    function = '3*x+4'
    for i in Df:
        listoffunction = list(function) #Will return ['3', '*', ...]
        for obj in listoffunction:
            if obj == 'x' :
                obj = i
        resultfori = ''
        for part in listoffunction:
            resultfori.join(part) #until here everything is fine.
                                  #resultfori is the following: '3*i+4'
                                  #with i the number of Df

Here is my problem: How can I calculate this term? I've tried it with the exec command but it always returns None. Why? I want it for example if i is 3 to return 13. Can you help me?

Thanks a lot!

h_e_u_r_e_k_a
  • 115
  • 2
  • 2
  • 21

1 Answers1

0

try something like this:

x = input()
s = '3*x + 4'
print eval(s)
Sanindra
  • 70
  • 7