# Turning a string into an programatic mathematical expression

Discussion in 'Python' started by Daniel Bickett, Oct 20, 2004.

1. ### Daniel BickettGuest

The title really says it all. I'm trying to take input from a user
(intended to be a mathematical expression), from a text box for
example, and evaluate it mathematically within the program. For
clarification: the user inputs the string "4*5(3-3)", I would be
interested in a straight-forward way to find the result of that, based
only on a string. The follow-up question would be how to incorporate
variables into the mix, however I'll leave it at that for now. Thanks

Daniel Bickett

Daniel Bickett, Oct 20, 2004

2. ### Steve HoldenGuest

Daniel Bickett wrote:

> The title really says it all. I'm trying to take input from a user
> (intended to be a mathematical expression), from a text box for
> example, and evaluate it mathematically within the program. For
> clarification: the user inputs the string "4*5(3-3)", I would be
> interested in a straight-forward way to find the result of that, based
> only on a string. The follow-up question would be how to incorporate
> variables into the mix, however I'll leave it at that for now. Thanks
>
> Daniel Bickett

Well, you can simply use input(), a horrendously dangerous function that
was designed (if that's the right word) in less security-minded times to
allow users to enter expressions which would be made available to the
program:

>>> print input("What: ")

What: 4*5*(3-3)
0
>>> print input("What: ")

What: 24+35/7
29
>>>

Don't know whether this will help. It's also possible to use variables

>>> a=33
>>> b=15.5
>>> print input("What: ")

What: a/b
2.12903225806
>>>

Note that the inputs must be valid Python expressions, which
unfortunately removes the possiblity of your implied multiplication:

>>> print input("What: ")

What: 4*5(3-3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 0, in ?
TypeError: 'int' object is not callable
>>>

If this isn't going to help you then I'm afraid you'll have to get down
and dirty by parsing the expressions and evaluating them in detail.

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119

Steve Holden, Oct 20, 2004

3. ### Steve HoldenGuest

Daniel Bickett wrote:

> The title really says it all. I'm trying to take input from a user
> (intended to be a mathematical expression), from a text box for
> example, and evaluate it mathematically within the program. For
> clarification: the user inputs the string "4*5(3-3)", I would be
> interested in a straight-forward way to find the result of that, based
> only on a string. The follow-up question would be how to incorporate
> variables into the mix, however I'll leave it at that for now. Thanks
>
> Daniel Bickett

I should, perhaps, have explained that the input() built-in essentially
applies the eval() function to an input string. So, whatever the source
of your string you can use eval() to evaluate it.

The difficulty is that there's little control over what the user can
enter (though you do get the choice of providing dictionaries of local
and global variables it's hard to limit what users have access to and
still provide sufficient functionality).

>>> eval('"Hello" + " " + "world"')

'Hello world'
>>> eval("3+14/27.4")

3.5109489051094891
>>>

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119

Steve Holden, Oct 20, 2004
4. ### Paul McGuireGuest

"Daniel Bickett" <> wrote in message
news:...
> The title really says it all. I'm trying to take input from a user
> (intended to be a mathematical expression), from a text box for
> example, and evaluate it mathematically within the program. For
> clarification: the user inputs the string "4*5(3-3)", I would be
> interested in a straight-forward way to find the result of that, based
> only on a string. The follow-up question would be how to incorporate
> variables into the mix, however I'll leave it at that for now. Thanks
>
> Daniel Bickett

This is a pretty standard text processing task, often assigned as homework
in CS classes. Check out this entry from the Python Tutor list
http://mail.python.org/pipermail/tutor/2003-December/027032.html, authored

You can also find a working Python expression parser included with the
examples provided with the pyparsing parser module, to be found at
http://pyparsing.sourceforge.net .

-- Paul McGuire

Paul McGuire, Oct 20, 2004