Why am i getting an "invalid syntax" when i run below code. Python 2.7
from string import *
def countSubStringMatch(target,key):
counter=0
fsi=0 #fsi=find string index
while fsi<len(target):
fsi=dna.find(key,fsi)
if fsi!=-1:
counter+=1
else:
开发者_开发问答 counter=0
fsi=fsi+1
fsi=fsi+1
#print '%s is %d times in the target string' %(key,counter)
def countSubStringMatch("atgacatgcacaagtatgcat","atgc")
In the line:
def countSubStringMatch("atgacatgcacaagtatgcat","atgc")
You should remove the def
. def
is used when defining a function, not when calling it.
for string count you could just do:
target = "atgacatgcacaagtatgcat"
s = 'atgc'
print '%s is %d times in the target string' % (s, target.count(s))
Other things wrong with your code:
You don't use and don't need anything in the string module. Don't import from it.
Don't do
from somemodule import *
unless you have a very good reason for it.Your code rather slowly and pointlessly struggles on after the first time that
find
returns -1 ... your loop should includeif fsi == -1: return counter
so that you return immediately with the correct count.
Be consistent: you use
counter += 1
butfsi = fsi + 1
... which reminds me: find 'PEP 8' (style guide) at www.python.org, and read it -- your space bar must be feeling unloved ;-)
HTH
John
精彩评论