这段代码出了什么问题?

时间:2010-08-16 15:56:47

标签: python search substring

代码是搜索子字符串...代码接收2个输入...第2个字符串用于搜索...即第2个字符串的长度较短。

a=input("Enter the 1st string")   //Giving error here
b=input("Enter the second string")

com=""

for x in range(0,len(a)):
       com=""
       for j in range(x,len(b)+x):
                com=com+a[j]
       if(com==b):
       print "Match Found at" + str(x)
       else:
       continue

代码doest编译....请帮助

2 个答案:

答案 0 :(得分:4)

如果您使用的是Python 2.x,则需要使用raw_input,而不是inputinput尝试评估您输入的内容,就像它是Python代码一样。这在Python 3中已不再适用。

另一个显而易见的事是:

if(com==b):
print "Match Found at" + str(x)
else:
continue

...需要像这样缩进:

if(com==b):
    print "Match Found at" + str(x)
else:
    continue

答案 1 :(得分:4)

相关问题