繁体   English   中英

如何在Python中将字符串转换为代数或超越函数?

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

此代码是wxglade代码,它接受文本框的输入,是代数或先验函数

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

它给出了错误

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

无论您输入text_ctrl_1中的任何Python代码都引用未定义的变量d。 Python不了解代数或符号数学; 如果您将x = y + 3类的字符串传递给eval ,并且xy都未定义,Python将抛出错误。

但是,有一些库可以在Python中进行符号数学运算。 如果要在Python中使用符号数学,请查看SymPy

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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