How do I even complete Northwest?

Even though there are no errors in the code, my baby griffin will just float away mindlessly and forget to tell the peasants, leaving me to die.

# Your pet should find and then bring the potion to the hero.

# This function checks if the word is in the text.
def wordInText(text, word):
    # Iterate through each character in the text.
    for i in range(len(text) - len(word) + 1):
        # For each of them loop through each character in word.
        for j in range(len(word)):
            # Store the shifted index i + j.
            shiftedIndex = i + j
            # If a character within the shifted index.
            # isn't equal to the character in word at the index "j"
            if text[shiftedIndex] != word[j]:
                # Break the loop.
                break
            # If j is equal to the index of the last letter in word
            if shiftedIndex == len(text) - len(word) - 1:
                # Then the entire word is in the text.
                # Return True.
                return True
    # The word was not found in text. Return False.
    return False

# Follow the guides directions where to run.
def onHear(event):
    # If "west" is in the phrase, the pet should run left.
    if wordInText(event.message, "west"):
        pet.moveXY(pet.pos.x - 28, pet.pos.y)
    # If "north" is in the phrase, the pet should run up.
    elif wordInText(event.message, "north"):
        pet.moveXY(pet.pos.x, pet.pos.y + 24)
    # Else the pet should try to fetch the potion.
    else:
        life = pet.findByType("potion")
        potion = pet.findNearest(life)
        if potion and pet.distanceTo(poiton) < 5:
            pet.fetch(potion)

pet.on("hear", onHear)
1 Like

Pets don’t have this method. Not sure why you changed the default code. It was:

potion = pet.findNearestByType("potion")

if shiftedIndex == len(text) - len(word) - 1:

This line is incorrect.

  1. The comments say that if “j” is equal to the index of the last letter in “word”, then return true. “j” isn’t shiftedIndex. shiftedIndex - i is “j”.
  2. This should only involve “j” and something about “word”. It should not include anything about “text”.
  3. An example:
    “almost” is now our target word. almost[0] is “a”, almost[1] is “l”, and so on. But the length of the word doesn’t start on 0. So the length of “almost” is 6. We need to find the index of the last letter of the word. almost[5] is “t”, which is also the last letter of “almost”. What do you find? len(almost) - 1 is 5, and also the index of the last letter of the word.
    PS: I found that - 2 or - 3 also works for this level, for some reasons that I don’t know of.
    Hopefully this would be helpful to whoever came across this.