-1

Can you make a variable into an equation so it can be solved?

A simple example would be something like

equ = "23/(n+2)"

for n in range(2):
    ans = equ
    print ans

This returns

23/(n+2)
23/(n+2)

Rather than the desired

11.5
7.667
Murray
  • 9
  • 1
  • 1
    Making a bigot of myself, this won't "solve" anything. That merely "evaluates" an expression. – Sylvain Leroux Aug 21 '14 at 14:13
  • To elaborate on @Sylvain's comment: You're not trying to solve an equation. You apparently want to compute the value of an expression with one variable in it for different values of that variable. In this case truly "solving the equation" would be something like determining which value(s) of `n`, if any, gave a specified result. – martineau Aug 21 '14 at 15:20

2 Answers2

2

You could use SymPy for this:

from sympy import sympify, Symbol

equ = "23/(n+2)"
equ_ = sympify(equ)
n_ = Symbol('n')

for n in range(2):
    print equ_.subs({n_: n}).evalf()
wim
  • 338,267
  • 99
  • 616
  • 750
  • 2
    That's a much neater solution than either in the linked duplicate. – Charles Clayton Aug 21 '14 at 14:51
  • Thanks. That's the problem with new questions being closed to years old duplicates ... – wim Aug 21 '14 at 14:52
  • @wim _"That's the problem with new questions being closed to years old duplicates"_ What should be the appropriate way of handling this: Reopen this question? Close the old one as duplicate of this one? – Sylvain Leroux Aug 21 '14 at 15:30
  • Good question. I don't know the answer. It was discussed here http://meta.stackexchange.com/q/97076/162650 – wim Aug 21 '14 at 15:42
  • 1
    sympy is not safe from eval-like hacks though – wim Nov 04 '14 at 18:25
-2

Note: eval is really dangerous.

You could use eval():

equ = "23/(n+2)"

for n in range(2):
    ans = equ
    print eval(ans)

Note that this will give incorrect results in Python 2 (11 and 7 respectively). In Python 3 it would give the correct result. You can fix that in Python 2 by making sure at least one involved number is not an integer:

equ = "23.0/(n+2)"

for n in range(2):
    ans = equ
    print eval(ans)
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • 5
    You could use `eval`, but you shouldn't in most cases. Please **always** state the security and performance implications of `eval` when suggesting it, as well as recommending the alternatives such as `ast.literal_eval` or simply writing a small parser. – l4mpi Aug 21 '14 at 14:08
  • @l4mpi: `ast.literal_eval` won't work in this case as the string doesn't contain a literal. Writing an expression parser is not trivial either (depending on skill level). – Simeon Visser Aug 21 '14 at 14:11
  • `ast.literal_eval` is not appropriate here – wim Aug 21 '14 at 14:11
  • 1
    Python 2 is not giving incorrect results -- they are exactly what the [documentation](https://docs.python.org/2/reference/expressions.html?highlight=division#binary-arithmetic-operations) says will happen. The result will be different in Python 3 because the meaning of the `/` division operator was changed. One can make version 2 behave like version 3 in this respect by simply by adding a `from __future__ import division` at the beginning of the script. – martineau Aug 21 '14 at 14:21
  • @martineau: What you're saying is correct but only because we're used to that behaviour in other languages. It can be argued that the behaviour in Python 2 was incorrect and that's why it was changed in Python 3. Division of `11 / 3` should give a fraction, not `3`. – Simeon Visser Aug 21 '14 at 14:34
  • The behavior in Python 2 is correct in the sense that in that version (as in many other programing languages, as you point out) the result of the division operator depends on the type of its operands. About all one can say is that in Python 3 the computed result is now more consistent (although at the same time it's now inconsistent with all those other existing languages not to mention a huge number of existing Python 2 scripts). – martineau Aug 21 '14 at 14:51
  • @martineau: the result of the division operator doesn't depend on the type of its operands, that's only a limitation of a digital computation (mostly for historical reasons). Compare it with divisions done in school, on paper, etc. – Simeon Visser Aug 21 '14 at 15:06
  • It's simply whole-number arithmetic. – martineau Aug 21 '14 at 16:28