[SOLVED] Pls help me i am kind of stuck at this level : timber guard

Continuing the discussion from Timber Guard help for python:

pls timber guard is get on my nerds i have try possible codes and even read the hint someone pls help me out

Could you show us your code formated how it is described here?

1 Like

while True:
# Collect gold.
item = hero.findNearestItem()
if item:
hero.move(item.pos)
# If you have enough gold, summon a soldier.
if hero.gold > hero.costOf(“soldier”):
hero.summon(“soldier”)
hero.command(soldier, “move”, {“x” : 75, “y” : 49})
# Use a for-loop to command each soldier.
# For loops have two parts: “for X in Y”
# Y is the array to loop over.
# The loop will run once for each item in Y, with X set to the current item.
for friend in hero.findFriends():
if friend.type == “soldier”:
enemy = friend.findNearestEnemy()
# If there’s an enemy, command her to attack.
# Otherwise, move her to the right side of the map.
if enemy:
hero.summon(“soldier”)
hero.command(soldier, “attack”, enemy)
else:
item = hero.findNearestItem()
if item:
hero.move(item.pos)

while True:
# Collect gold.
item = hero.findNearestItem()
if item:
hero.move(item.pos)
# If you have enough gold, summon a soldier.
if hero.gold > hero.costOf(“soldier”):
hero.summon(“soldier”)
hero.command(soldier, “move”, {“x” : 75, “y” : 49})
# Use a for-loop to command each soldier.
# For loops have two parts: “for X in Y”
# Y is the array to loop over.
# The loop will run once for each item in Y, with X set to the current item.
for friend in hero.findFriends():
if friend.type == “soldier”:
enemy = friend.findNearestEnemy()
# If there’s an enemy, command her to attack.
# Otherwise, move her to the right side of the map.

You did not format the code. Here is the topic that explains how to format it:

1 Like

It will be the only time I will format the code for you. Here is the code formated:

while True:
    # Collect gold.
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    # If you have enough gold, summon a soldier.
    if hero.gold > hero.costOf(“soldier”):
        hero.summon(“soldier”)
    hero.command(soldier, “move”, {“x” : 75, “y” : 49})
    # Use a for-loop to command each soldier.
    # For loops have two parts: “for X in Y”
    # Y is the array to loop over.
    # The loop will run once for each item in Y, with X set to the current item.
    for friend in hero.findFriends():
        if friend.type == “soldier”:
            enemy = friend.findNearestEnemy()
            # If there’s an enemy, command her to attack.
            # Otherwise, move her to the right side of the map.

Try to put after this

An if enemy and command the soldier to attack inside it with this:

hero.command(friend,"attack",enemy)

Inside the if statement. Put an else to the if enemy and put inside it this:

And delete this line from where it was in your code.
Do you need any more assistance at this level?

1 Like

Have you completed the level?

1 Like

yes
and i also had to switch the soldier with friends
thank you very much

2 Likes

No problem! Glad I could help you! And congratulations for completing the level! :partying_face:

1 Like