如何在 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论您在 text_ctrl_1 中放入什么 Python 代码,都会引用未定义的变量 d。 Python 不理解代数或符号数学;如果将
x = y + 3
之类的字符串传递给eval
并且x
或y
未定义 Python会抛出错误。然而,有一些库可以在 Python 中进行符号数学运算。如果您想在 Python 中使用符号数学,请查看 SymPy。
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
toeval
and eitherx
ory
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.