简体   繁体   中英

How do I convert a string into an algebraic or transcendental function in Python?

This code is a wxglade code & it takes the input of a text box whis is a algebraic or transcendental function

 def event_2(self, event): # wxGlade: application.<event_handler>
        #print "Event handler `event_2' not implemented!"
        #event.Skip()
    equationString = self.text_ctrl_1.GetValue()
    function = eval(equationString)
    event.Skip()

it gives the error

Traceback (most recent call last):
  File "ppy.py", line 103, in event_2
    function = eval(equationString)
  File "<string>", line 1, in <module>
NameError: name 'd' is not defined

Whatever Python code you've put in text_ctrl_1 refers to a variable d that wasn't defined. Python does not understand algebra or symbolic math; if you pass a string like x = y + 3 to eval and either x or y are not defined Python will throw an error.

There are, however, libraries to do symbolic math in Python. If you want to use symbolic math in Python, look in to SymPy .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM