无法打印 stringMatching 模式
Can't print the stringMatching pattern
当我 运行 这段代码时,我没有收到任何错误,但我无法在下面打印出我的代码。
我试过改变条件,但没有成功。我的错误在哪里以及如何解决?
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]))
假设您要查找子字符串,您可以使用如下所示的 find() 方法:
print(string.find(substr))
如果结果>-1,则为子串。如果你只想要一个 return 0/ -1,你也可以创建一个函数,如下所示:
string = "Chapter I. The quick brown fox jumped over the lazy dog."
substr = "over the"
substr2 = "lazy hat"
def checkSubstring(string, substring):
if string.find(substring) > -1:
print("Within the Text")
return 0
else:
print("Not within the Text")
return -1
checkSubstring(string, substr)
checkSubstring(string, substr2)
其中 return 分别为 0 和 -1
当我 运行 这段代码时,我没有收到任何错误,但我无法在下面打印出我的代码。
我试过改变条件,但没有成功。我的错误在哪里以及如何解决?
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]))
假设您要查找子字符串,您可以使用如下所示的 find() 方法:
print(string.find(substr))
如果结果>-1,则为子串。如果你只想要一个 return 0/ -1,你也可以创建一个函数,如下所示:
string = "Chapter I. The quick brown fox jumped over the lazy dog."
substr = "over the"
substr2 = "lazy hat"
def checkSubstring(string, substring):
if string.find(substring) > -1:
print("Within the Text")
return 0
else:
print("Not within the Text")
return -1
checkSubstring(string, substr)
checkSubstring(string, substr2)
其中 return 分别为 0 和 -1