Help with backwoods fork

https://codecombat.com/play/level/backwoods-fork?

Hello I’m still a beginner in Code Combat so maybe one of you more experienced users could point me in the right direction. As you will see in this level you need to patrol moving from X to X. My problem is that for some reason ( that might be obvious ) I can’t make my character move to the bottom X. I have tried to change the order and re-writing my code but I still have the same problem.
Please let me know if you can’t go to the level or if you do not understand something this is my first post so there could be an error. Thanks! :grinning:

Post your code so we can help you.

can’t you use the link?

No. I won’t be able to see your code.

ok, here it is:


def checkAndAttack(target):
# The ‘target’ parameter is just a variable!
# It contains the argument when the function was called.
if target:
hero.attack(target)
hero.moveXY(43, 34)

while True:
hero.moveXY(58, 52)
topEnemy = hero.findNearestEnemy()
# Using the checkAndAttack function with the topEnemy variable.
checkAndAttack(topEnemy)

while True:
# Move to the bottom X mark.
hero.moveXY(58, 16)
# Create a variable named bottomEnemy and find the nearest enemy.
bottomEnemy = hero.findNearestEnemy()
# Use the checkAndAttack function, and include the bottomEnemy variable.
checkAndAttack(topEnemy)

Use the </> button so I can see the format.

Also you haven’t defined target.

def checkAndAttack(target):
    # The 'target' parameter is just a variable!
    # It contains the argument when the function was called.
    if target:
        hero.attack(target)
    hero.moveXY(43, 34)

while True:
    hero.moveXY(58, 52)
    topEnemy = hero.findNearestEnemy()
    # Using the checkAndAttack function with the topEnemy variable.
    checkAndAttack(topEnemy)

while True:
    # Move to the bottom X mark.
    hero.moveXY(58, 16)
    # Create a variable named bottomEnemy and find the nearest enemy.
    bottomEnemy = hero.findNearestEnemy()
    # Use the checkAndAttack function, and include the bottomEnemy variable.
    checkAndAttack(topEnemy)

what do you mean I haven’t defined target?

You don’t need to define a variable when it’s inside a function (if the variable is contained as an argument within the function’s brackets). He is defining target here:

It’s just under the name of topEnemy.
:lion: :lion: :lion:

1 Like

thanks so just to clarify: what is the problem?

The reason your hero isn’t moving to the bottom X is because you’ve used two while true loops. Unless you use something to stop a while true loop, e.g. break (which is covered in later levels), then the code in the second while true loop won’t play.
:lion: :lion: :lion:

I see so how do I stop the true loop with out e.g. break?

Sorry, that’s not what I meant.
You don’t need two while true loops at all. In fact if you use two it won’t work (even with break).
:lion:

1 Like

oh I get it so I just have to use 1
thank you both very much!:call_me_hand::call_me_hand::+1::ok_hand:

@Deadpool198 thank you for taking the time to help me with my problem. Here in my country Chile I don’t know anyone who knew how to do this, so thank you again

That’s fine, my pleasure.
:lion: :lion: :lion:

1 Like

@ducky
here is my code i need help really bad

# This shows how to define a function called cleaveWhenClose
# The function defines a parameter called `target`
def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        pass
        # Put your attack code here
        # If cleave is ready, then cleave target
        while True:
            enemies = hero.findEnemies()
        if enemy:
            hero.isReady("cleave")
            hero.cleave(enemies)
        # else, just attack `target`!
        hero.attack(enemy)
        hero.attack(enemy)

# This code is not part of the function.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Note that inside cleaveWhenClose, we refer to the `enemy` as `target`.
        cleaveWhenClose(enemy)


this is python and i cannot get pass the first wave of orges

You don’t need the while True in the function because the function is already in the loop. Also your hero.isReady('cleave') is not in a if statement.
Here’s what I would do

if target:
    if hero.isReady('cleave'):
        hero.cleave(target)
    else:
        hero.attack(target)

@fireball could you please explain that to me a little bit more in a DM?

@dedreous
i need help so badly on this level

Zax, post your code again please…let’s see exactly what you have now.