我想计算字符串s中发生“bob”的次数。我写的代码是:python - 计算字符串失败的连续字母集合的程序
s = 'qwbobthghdeerxybobhjkhgkjgbob'
num = 0
count = 0
for char in s:
if char == 'b':
letter = s[num+1]
if letter == 'o':
letter = s[num+2]
if letter == 'b':
count = count + 1
num += 1
print('Number of times bob occurs is:' + str(count))
运行代码给出了错误:
Traceback (most recent call last):
File "C:/Python27/practice.py", line 6, in <module>
letter = s[num+1]
IndexError: string index out of range
变量num的壳价值出来作为
>>>num
28
这怎么可能?