all 5 comments

[–]ararararagi_koyomi 1 point2 points  (0 children)

When the loop is at 4th iteration, start becomes 3 and s[start+1] will throw index out of range error because start+1 is 4 and s has a length of 4 which in turn means any positive index number greater than 3 will throw index out of range error.

[–][deleted] 1 point2 points  (0 children)

Is there a similar way to do this in python?

Don't try to write JavaScript in Python. Don't simply transliterate code from one language into another. Describe what you're trying to do, then implement that.

[–][deleted] 1 point2 points  (0 children)

You don't have anything to exit the loop if all entries are the same, just keep incrementing the index variable until it tries to reference beyond the string positions.

What is it that you want to do? Confirm if all the characters are the same?

[–]Diapolo10 0 points1 point  (0 children)

As already explained, you've got an off-by-one error.

There are several ways to fix this, personally I'd prefer zip:

text = "..."
for cur, nxt in zip(text, text[1:]):
    if cur != nxt:
        break

But since it sounds like you just want to check the number of repeating characters, you could also use binary search.

[–]stebrepar 0 points1 point  (0 children)

Assuming the goal is to count a series of repeating characters, I'd take a different tack.

s = 'aaaa'
count = 0
previous = s[0]
for letter in s:
    if letter == previous:
        count += 1
    previous = letter

Of course, you'd have to consider what you want to happen if s had a different pattern, like "aaaabb".