Turning a string into an programatic mathematical expression

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

  1. 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
    Daniel Bickett, Oct 20, 2004
    #1
    1. Advertising

  2. Daniel Bickett

    Steve Holden Guest

    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
    Steve Holden, Oct 20, 2004
    #2
    1. Advertising

  3. Daniel Bickett

    Steve Holden Guest

    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
    Steve Holden, Oct 20, 2004
    #3
  4. Daniel Bickett

    Paul McGuire Guest

    "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
    > 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
    Paul McGuire, Oct 20, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Tmljaw==?=

    Programatic Insantiation of WebForms

    =?Utf-8?B?Tmljaw==?=, Jan 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    305
    =?Utf-8?B?Tmljaw==?=
    Jan 16, 2004
  2. John Hunter
    Replies:
    0
    Views:
    355
    John Hunter
    Oct 20, 2004
  3. kamlesh
    Replies:
    0
    Views:
    400
    kamlesh
    Jun 13, 2007
  4. kamlesh

    Update of DetailsView with programatic DataBinding

    kamlesh, Jun 13, 2007, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    322
    kamlesh
    Aug 1, 2007
  5. Simon
    Replies:
    1
    Views:
    109
    Anthony Jones
    Sep 7, 2007
Loading...

Share This Page