I cannot clear VILLAGE CHAMPION

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.

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.

4 Likes

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()

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

can you help me whot my code this is my code

def cleaveOrAttack():
    # In de functie: vind een vijand en cleave of val hem aan.
    ogre = hero.findNearestEnemy()
    if ogre:
        if hero.isReady("cleave"):
            hero.cleave(ogre)
        # Else attack the ogre:
        else:
            hero.attack(enemy)

# Loop heen en weer tussen de patrouillepunten en roep de functie aan.
while True:
    hero.moveXY(35, 34)
    # Gebruik de functienaam die je hier boven hebt meegegeven.
    cleaveOrAttack
    hero.moveXY(47, 27)
    # Roep de functie weer aan.
    cleaveOrAttack
    hero.moveXY(60, 31)
    # Roep de functie weer aan.
    cleaveOrAttack

When you call a function, you always need to put brackets () on the end, just like when you defined it:

Otherwise the compiler (the thing that reads your code) won’t know that what you’re running is a function.
Danny

do i need to type def to?

thx deadpool i reached it