2016-03-02 133 views
-2

這是我寫的Vim的Python代碼。每當我運行它,我得到的錯誤'str' object has not attribute 'subs'「海峽」對象沒有屬性「潛艇」

from sympy import * 
x,a_test,b_test,fa_test,fb_test=symbols('x a_test b_test fa_test fb_test') 
expr=raw_input("enter the equation") 
print expr 
print "hello" 
try: 
    print "hello" 
    inc=0 
    a=inc 
    fa=expr.subs(x,inc) 
    print "hello" 
    if(fa<0): 
     print "hello" 
     inc+=1 
     fb=expr.subs(x,inc) 
     if(fb<=0): 
      while(fb<=0): 
       inc+=1 
    else: 
     print "hello" 
     inc+=1 
     fb=expr.subs(x,inc) 
     if(fb<=0): 
      while(fb<=0): 
       inc+=1 
    b=inc 
    print a 
    print b 
    print fa 
    print fb 
except Exception,e: 
    print e 
+1

你在期待'subs'辦?這不是'str'的​​方法,'expr'是(從'raw_input'返回的)。 – Kupiakos

+0

你可能尋找['str.replace'(https://docs.python.org/2/library/stdtypes.html#str.replace)。請注意,替換也必須是'str'類型(而不是'inc'定義的'int')。 – metatoaster

+0

我期待從潛水艇替代將在輸入用戶的公式中的「inc」的值代替x的值。 –

回答

1

raw_input返回值是一個str;您不能像使用SymPy expr一樣使用它。首先,您需要parse它:

from sympy.parsing.sympy_parser import parse_expr 
... 
expr = parse_expr(raw_input("enter the equation")) 
+0

親愛Seluck ......「NegativeOne」對象沒有屬性「條」 ......請問這個意思? –

+0

它必須與你的表情有關。你輸入什麼作爲等式? – Selcuk

+0

參見下面是我修改後的代碼爲u建議我把土特產品的事情parse_expr,我輸入方程x ** 2-X-1 –

相關問題