【发布时间】:2020-03-02 04:30:28
【问题描述】:
当我运行此代码时,我没有收到任何错误,但我无法在下面打印出我的代码。
我尝试改变条件位置改变但没有奏效。我的错误在哪里以及如何解决?
def stringMatching(text, pattern):
for i in range (len(text) - len(pattern)):
j = 0
while j < len(pattern) & pattern[j] == text[i+j]:
j = j + 1
if j == len(pattern):
return -1
string = "Chapter I. The quick brown fox jumped over the lazy dog."
substr = "over the"
print(stringMatching([string],[substr]))
【问题讨论】:
-
如果满足
j==len(pattern),您的函数可能返回值是-1,或者None。您可能想返回一些东西以便您可以打印它。您还需要修复上面代码中的缩进,因为这显然是不正确的。
标签: python algorithm string-matching