Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Turning a string into an programatic mathematical expression

Reply
Thread Tools

Turning a string into an programatic mathematical expression

 
 
Daniel Bickett
Guest
Posts: n/a
 
      10-20-2004
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
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      10-20-2004
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
> 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:

>>> 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
in your expressions:

>>> 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
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      10-20-2004
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
> 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).

>>> 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
 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      10-20-2004
"Daniel Bickett" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/tut...er/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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting programatic feedback from an smtp server Simon ASP General 1 09-07-2007 07:16 PM
Update of DetailsView with programatic DataBinding kamlesh ASP .Net Web Controls 2 08-01-2007 10:15 PM
Update of DetailsView with programatic DataBinding kamlesh ASP .Net 0 06-13-2007 05:07 PM
Re: Turning a string into an programatic mathematical expression John Hunter Python 0 10-20-2004 02:17 AM
Programatic Insantiation of WebForms =?Utf-8?B?Tmljaw==?= ASP .Net 0 01-16-2004 09:31 AM



Advertisments