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 - findreturns -1 ... your loop should include- if fsi == -1: return counter- so that you return immediately with the correct count. 
- Be consistent: you use - counter += 1but- fsi = 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
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论