Bob Counter in Python

后端 未结 8 1324
名媛妹妹
名媛妹妹 2021-01-28 11:25

Using Python 2.7, I was attempting to count the number of occurances of \'bob\' in the phrase \'bobbbobobboobobookobobbobbboj.\' To do this, I wrote the code below:



        
8条回答
  •  旧巷少年郎
    2021-01-28 11:58

    start=0
    count=0 
    while start<=len(s):
        n=s.find('b',start,len(s))
        prnt=(s[start:start+3])
        if prnt =='bob':
           start=n+2 
           count+=1
        else:
            start+=1        
    print count   
    

提交回复
热议问题