I’ve been stuck on this level for a long time, can someone help me?

Post the code you have so far and we’ll be glad to help. Please be sure to post your code properly using the </> button or post a screen shot so we can see the structure. Thanks.

1 Like

Here’s my code:

# The function maybeBuildTrap defines TWO parameters!

def maybeBuildTrap(x, y):
# Use x and y as the coordinates to move to.
hero.moveXY(x, y)
enemy = hero.findNearestEnemy()
if enemy:
# Use buildXY to build a “fire-trap” at the given x and y.
hero.buildXY(“fire-trap”, enemy.pos.x, enemy.pos.y)

while True:
# This calls maybeBuildTrap, with the coordinates of the top entrance.
maybeBuildTrap(43, 50)

``````# Now use maybeBuildTrap at the left entrance!
maybeBuildTrap(25, 34)
# Now use maybeBuildTrap at the bottom entrance!
maybeBuildTrap(43, 20)``````
1 Like

I’ve seen a post about someone with the similar problem before, but I didn’t understand the explanation.

1 Like

Please learn to post your code properly from the game from now on and use the </> button or it won’t format properly. When you click the </> button, the following will appear:

Please paste ALL of your code inside the triple back tick marks. There are many people here willing and able to help. If you use the </> button correctly, then ALL of your code should look like this:

``````while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
else:
hero.say("My code is formatted properly")
``````

If ALL of your code doesn’t look like the code above, then you’re doing it wrong and we can’t see the structure of the code to troubleshoot whether or not that is the issue. Use the </> button and help us help you. This works when sending a private message as well. Thank you.

1 Like

It didn’t work for me, sorry.

Nevermind, I’ll look back on the level later on.

1 Like

It works for everyone. Your account is no different from anyone else’s. Just put all of your code in between the triple back ticks that appear when you click on the </> button.

# ``` <— Triple back tick marks.

Paste ALL of your code in here.

# ``` <— Triple back tick marks.

1 Like
``````# The function maybeBuildTrap defines TWO parameters!
def maybeBuildTrap(x, y):
# Use x and y as the coordinates to move to.
hero.moveXY(x, y)
enemy = hero.findNearestEnemy()
if enemy:
# Use buildXY to build a "fire-trap" at the given x and y.
hero.buildXY("fire-trap", enemy.pos.x, enemy.pos.y)

while True:
# This calls maybeBuildTrap, with the coordinates of the top entrance.
maybeBuildTrap(43, 50)

# Now use maybeBuildTrap at the left entrance!
maybeBuildTrap(25, 34)
# Now use maybeBuildTrap at the bottom entrance!
maybeBuildTrap(43, 20)

``````
1 Like

I knew you could figure it out.

In your function, you are telling your hero to build a fire trap at the enemy position. That’s not a good idea because the enemy is moving. You have coordinates in the while True loop that tell you where to put the fire traps. In arguments of your function, all you need to do is put x and y. Then when you call the function, it will use the x and y that you have defined there.

1 Like

Thanks very much, it was a great help :)

1 Like

Put [SOLVED] so nobody thinks it isn’t solved.

2 Likes

Ok it’s done now, thanks

2 Likes

Mod edit: Solution removed

1 Like

Does that mean that you have “creatively coded” the level so you win?

i have it i have the code delete every thing off your coding and copy and paste this right here # Patrol the village entrances.

Mod edit: Please do not post solutions.

Hi @Andre_Boudreaux, welcome to the codecombat discourse.
Thanks you for helping out, but we would rather you didn’t post the complete solution to a level, as it can ruin it for others and is not the purpose of the forum. Please feel free to help with advice, suggestions, etc. But giving another person the solution won’t help them learn.
If you could delete that post (click the three dots next to the heart and the chain at the bottom of the post and press the bin) that would be great.
Thank you very much, and welcome again!
Danny

plz help it wont let me just put x and y, it says it got a null.
help plz!