【发布时间】:2018-03-10 17:22:07
【问题描述】:
假设 s 是一串小写字符。 编写一个程序,打印字符串 'bob' 在 s 中出现的次数。例如,如果 s = 'azcbobobegghakl',那么程序应该打印 bob出现的次数是:2
我在下面写了这个程序。
s = 'azcbobobegghakl'
count = 0
if(s.find("b")):
p = s.index('b')
while p+2 <= len(s):
if(s[p+1] == 'o' and s[p+2] == 'b'):
count = count+1
p = s[p+2]
else:
p = s[p+1]
print (count)
但它在 while 循环中显示错误。但是如果我不使用 while 循环,它会运行没有任何错误。
【问题讨论】:
标签: python while-loop typeerror