[SOLVED]Why does adding a hero.say command break this code?

In trying to better understand the string search code in Northwest I added a hero.say command to have him display the current index number. He says it once and then the code stops executing. Without that say command it executes perfectly. What’s going on?

def wordInText(text, word):
    for i in range(len(text) - len(word) + 1):
        for j in range(len(word)):
            hero.say(j)
            shiftedIndex = i + j
            if text[shiftedIndex] != word[j]:
                break
            if j == len(word) -1:
                return True
    return False

Another thing I noticed when comparing this algorithm and the one from Highlanders is that the line

    for i in range(len(text) - len(word) + 1):

can be rewritten as

    for i in range(len(text) - len(word)):

and it still works perfectly. I don’t follow this either.

The reason (as far as I can tell) for the first problem is that you’re using a hero command inside the pet.on(“spawn”, onSpawn) function.
As I said to someone else recently, mixing hero and pet functions is not a great plan. I don’t think you can use hero.say() inside a function when it’s called inside the onSpawn() function. You can use pet.say() instead, it works perfectly (although you die because it takes too long, but I presume it’s only for debug anyway).
As for the second problem, that’s luck really. It should be + 1 because for loops stop looping at 1 number before the end number. So if you did:

for i in range(0, 10):
    hero.say(i)

I would print 10 numbers, but it would stop at 9.
Here, the same thing happens with:

It will miss out the last character of the message. But that’s ok, because it’s only either a full stop . or an exclamation mark ? which doesn’t affect the main message. However, it’s kind of bad practice to not include it, so I would go back to + 1.
I hope this helps.

That’s it. Thank you.

This could probably be closed now