簡體   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