Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Aggressive Mimicry

I need help with this level. Whenever a disguised ogre comes in, my hero waits until the ogre reveals his identity. Here’s my code:

def startsWith(text, word):
    # If the word is longer then the text:
    if len(word) > len(text):
        return False
    # Loop through the indexes of word and text.
    for index in range(len(word)):
        # If characters with the same index are different:
        if word[index] != text[index]:
            # Then the word doesn't coincide with the text.
            return False
    # We checked all letters and they are the same.
    return True

ogreNameStart = "Zog"

while True:
    suspectFriend = hero.findNearest(hero.findFriends())
    suspectName = suspectFriend.id
    # Use the function "startsWith" to check
    # if suspectName starts with "Zog":
    if startsWith(suspectFriend.id, ogreNameStart) == True:
        # Then attack suspectFriend:
        if enemy:
            hero.attack(suspectFriend)
    enemy = hero.findNearestEnemy()
    # if there is an enemy, then attack it:
    if enemy:
        hero.attack(enemy)
    # Else return to the red X mark:
    else:
        hero.moveXY(27, 27)

Why won’t it work?

Never mind. I took out the if enemy, and it worked then.