【发布时间】:2016-03-02 03:36:58
【问题描述】:
这是我用 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
【问题讨论】:
-
您希望
subs做什么?这不是str的方法,expr是(从raw_input返回)。 -
您可能正在寻找
str.replace。请注意,替换也必须是str类型(而不是为inc定义的int)。 -
我希望 subs 用“inc”的值代替 x 来代替用户输入的方程。
-
@ArsalanMehmood 请阅读documentation for Python's
strobject -
我刚刚编辑了代码并将 expr.subs 替换为 expr.replace 并将 inc 的数据类型从整数更改为字符串,但它对我不起作用..