Python IndexError:字符串索引超出范围

时间:2016-09-06 08:51:07

标签: python

有人可以帮助我为什么这么说:" IndexError:字符串索引超出范围" 当我添加" letterCount + = 1"对于第一个它,它会产生这个错误,没有它正在工作。

目标是在s中计算" bob"

谢谢!

s = 'oobobodobooobobobobabobbobbobobbobbobhbxbobbk'

vowelCount = 0
letterCount = 0
pointer = s

for pointer in s:  
    print(pointer)
    if pointer == 'b':
        print (str(letterCount) + '. betű B' )

        if (s[letterCount+1] + s[letterCount+2]) == str('ob') :
           vowelCount += 1
           letterCount += 1
           print( str(vowelCount) + '. BOB megtalálva')
        else:
           print('Nem OB jön utána')
           letterCount += 1
    else:      
        print(str(letterCount) + '. betű nem B')
        letterCount += 1

print ("Number of times bob occurs is: " + str(vowelCount))

3 个答案:

答案 0 :(得分:0)

您需要使用以下内容检查字符串的长度:

letterCount+2 <= len(s) 

s = 'oobobodobooobobobobabobbobbobobbobbobhbxbobbk'

vowelCount = 0
letterCount = 0
pointer = s

for pointer in s:  
    print(pointer)
    if pointer == 'b':
        print (str(letterCount) + '. betű B' )

        if (letterCount+2 <= len(s) and (s[letterCount+1] + s[letterCount+2]) == str('ob')) :
           vowelCount += 1
           letterCount += 1
           print( str(vowelCount) + '. BOB megtalálva')
        else:
           print('Nem OB jön utána')
           letterCount += 1
    else:      
        print(str(letterCount) + '. betű nem B')
        letterCount += 1

print ("Number of times bob occurs is: " + str(vowelCount))

答案 1 :(得分:0)

我希望以下代码适合您。

s = 'oobobodobooobobobobabobbobbobobbobbobhbxbobbk'

vowelCount = 0

letterCount = 0

pointer = s

print len(s)

for pointer in s:

    if pointer == 'b':

        if (len(s) != letterCount+1 and len(s) != letterCount+2):

            if (s[letterCount+1] + s[letterCount+2]) == str('ob'):

                vowelCount += 1

                letterCount += 1

                print(str(vowelCount) + '. BOB')

            else:

                letterCount += 1
    else:

        letterCount += 1


print ("Number of times bob occurs is: " + str(vowelCount))

在本声明中,我正在使用字符串的len检查字母数。它只会在字符串的末尾匹配。

或者您可以使用枚举器检查字符串中单词的len

for i, _ in enumerate(s): #i here is the index, equal to "i in 
range(len(s))"

if s[i:i+3] == 'bob': #Check the current char + the next three chars. bob += 1 print('Number of times bob occurs is: ' + str(bob))

答案 2 :(得分:0)

The final solution.

s = 'obbobbbocbobbogboobm'

vowelCount = 0
letterCount = 0
pointer = s

for pointer in s:  
    print(pointer)
    if pointer == 'b':
        print (str(letterCount) + '. betű B' )

        if (len(s)-2 > letterCount):      
            print('van utána két betű')
            if (s[letterCount+1] + s[letterCount+2]) == str('ob') :
               vowelCount += 1
               letterCount += 1
               print( str(vowelCount) + '. BOB megtalálva')
            else:
               print('Nem OB jön utána')
               letterCount += 1
        else:
           print('nincs utána két betű')           
           break
    else:      
        print(str(letterCount) + '. betű nem B')
        letterCount += 1

print ("Number of times bob occurs is: " + str(vowelCount))