views:

100

answers:

2

The code is to search for a substring...the code takes in 2 inputs...the 2nd string is used to search...i.e 2nd string is smaller in length.

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

The code doest compile....pls help

+4  A: 

If you are using Python 2.x, you need to use raw_input, not input. input tries to evaluate what you enter as though it is Python code. This is no longer true in Python 3.

Another obvious thing is that this:

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

... needs to be indented like this:

if(com==b):
    print "Match Found at" + str(x)
else:
    continue
Fred Larson
`raw_input` is only for Python 2.x; it's `input` in Python 3. I'd like to say give the OP the benefit of the doubt; but with the indenting errors I'm not so sure =)
katrielalex
thnx for the indentation....i changed input to raw_input ...still giving same compilation error at line 1
Vinod K
@Vinod K: What error?
katrielalex
@katrielalex: Yeah, I was editing to add the difference between Python 2 and 3 when you commented. And I agree, we really need to see the compile error.
Fred Larson
syntax error near unexpected token '('' a=input("Enter the 1st string")'
Vinod K
@Vinod K: Is this the entire code file? I wonder if you have an unmatched open paren before this first line.
Fred Larson
error at line 1..
Vinod K
i have this bfore the code file...#!/usr/bin/python
Vinod K
I typed it agn fresh....now its compiling....
Vinod K
@Vinod K: Yes, it worked for me after I changed to raw_input and put in the indentation. Then I run into a run-time error, but that's another matter.
Fred Larson
+3  A: 

b.find( a )

katrielalex
thnx...actually wanted to implement it in the above way only...i am new to python...
Vinod K