[英]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
,並且x
或y
都未定義,Python將拋出錯誤。
但是,有一些庫可以在Python中進行符號數學運算。 如果要在Python中使用符號數學,請查看SymPy 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.