代码工作正常,但在功能显示错误

时间:2012-01-18 10:04:54

标签: python jython grinder

我的代码有问题。我编写了一个函数来提取参数,通过发送原始字符串,预先/发布文本片段以及文本中出现的次数。当我用简单的代码编写它时效果很好:

searchstr = 'qwer1234asdfqwer5678asdfqwer1234asdfqwer5678asdf'
startstr = 'qwer'
endstr = 'asdf'
occurancenr = 4
start = searchstr.find(startstr)
print 'start = ' + str(start)
while start >= 0 and occurancenr > 1:
  start = searchstr.find(startstr, start + len(startstr))
  occurancenr -= 1
  print 'start = ' + str(start) + ', and nr = ' + str(occurancenr)
start = start + len(startstr)
end = searchstr.index( endstr, start )
print 'result = ' + searchstr[start:end]

但是当我把它放在返回字符串的方法中时,我得到错误:

继承人的职能:

  def findparam (self, searchstr, startstr, endstr, occurancenr):
  start = searchstr.find(startstr)
  while start >= 0 and occurancenr > 1:
    start = searchstr.find(startstr, start + len(startstr))
    occurancenr -= 1
  start = start + len(startstr)
  end = searchstr.index( endstr, start )
  return 'result = ' + searchstr[start:end]

以下是对此的要求:

abc = 'qwer1234asdfqwer5678asdf'
aa = 'qwer'
bb = 'asdf'
print self.findparam(abc, aa, bb, 1)

错误如下所示:

SyntaxError: ('invalid syntax', ('C:\\grinder-3.4\\scripts\\ResultCheckMerged.py', 367, 3, '  start = searchstr.find(startstr)'))
(no code object) at line 0

从技术上讲,这是一个运行grinder脚本的函数,比较器使用jython,但是,由于没有使用java代码和它只是python代码,我认为这是一个python问题。但是,也许它与此有关。

对此有任何提示赞赏!

2 个答案:

答案 0 :(得分:2)

您只需要正确缩进代码即可。下一个应该工作:

  def findparam (self, searchstr, startstr, endstr, occurancenr):
    start = searchstr.find(startstr)
    while start >= 0 and occurancenr > 1:
      ...

答案 1 :(得分:0)

不要缩进def line。


def findparam (self, searchstr, startstr, endstr, occurancenr):
      start = searchstr.find(startstr)
      while start >= 0 and occurancenr > 1:
        start = searchstr.find(startstr, start + len(startstr))
        occurancenr -= 1
      start = start + len(startstr)
      end = searchstr.index( endstr, start )
      return 'result = ' + searchstr[start:end]