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

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:

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:

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: