Turning a string into an programatic mathematical expression

D

Daniel Bickett

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
for your time :)

Daniel Bickett
 
S

Steve Holden

Daniel said:
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
for your time :)

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:
What: 4*5*(3-3)
0What: 24+35/7
29
Don't know whether this will help. It's also possible to use variables
in your expressions:
What: a/b
2.12903225806
Note that the inputs must be valid Python expressions, which
unfortunately removes the possiblity of your implied multiplication:
What: 4*5(3-3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?

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
 
S

Steve Holden

Daniel said:
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
for your time :)

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).

regards
Steve
 
P

Paul McGuire

Daniel Bickett said:
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
for your time :)

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
by Danny Yoo, which includes many helpful points about this problem.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top