I cannot clear VILLAGE CHAMPION


#1

Hi, there.

I have written the code below, but I could not clear the stage.

# Incoming munchkins! Defend the town!
enemy = hero.findNearestEnemy()
distance = hero.distanceTo(enemy)
# Define your own function to fight the enemy!
def cleaveOrAttack():
    # In the function, find an enemy, then cleave or attack it.
    if enemy:
        if hero.isReady("cleave"):
            hero.cleave(enemy)
        else:
            hero.attack(enemy)
    
    pass

# Move between patrol points and call the function.
while True:
    hero.moveXY(35, 34)
    # Use cleaveOrAttack function you defined above.
    cleaveOrAttack()
    hero.moveXY(47, 27)
    # Call the function again.
    cleaveOrAttack()
    hero.moveXY(60, 31)
    # Call the function again.
    cleaveOrAttack()

Could you kindly tell me how should I need modify the code.


#2

First, you are using global variables. This is usually considered a bad practice and should be avoided whenever possible. Place your variables where they are used, either inside your code loop or function. In this case, the variable enemy should be inside the function where it is being used.

Second, you define the variable distance but don’t use it. You should just delete it since it’s not needed.


#3

Thank you.

In this case, the variable enemy should be inside the function where it is being used.

This is the point which I should modify.

# Incoming munchkins! Defend the town!
# Define your own function to fight the enemy!
def cleaveOrAttack():
    # In the function, find an enemy, then cleave or attack it.
    enemy = hero.findNearestEnemy()
    if enemy:
        
        if hero.isReady("cleave"):
            hero.cleave(enemy)
        else:
            hero.attack(enemy)
    
    pass

# Move between patrol points and call the function.
while True:
    hero.moveXY(35, 34)
    # Use cleaveOrAttack function you defined above.
    cleaveOrAttack()
    hero.moveXY(47, 27)
    # Call the function again.
    cleaveOrAttack()
    hero.moveXY(60, 31)
    # Call the function again.
    cleaveOrAttack()

#4

Hi there You have to make up one code at the "if enemy"part