I am on the Rational Defense level and here is my code

``````# Protect the peasants.

# Move the peasants away from the woods.
def hideUnits(units):
for i in range(len(units)):
unit = units[i]
hero.command(unit, "move", {'x': 34, 'y': 10 + i * 12})

# The peasants know the order in which to build the traps.
peasants = hero.findFriends()
buildOrder = peasants[0].buildOrder
separator = ","
# Split buildOrder with a comma (",")
# and save the result to the variable "types":
types = buildOrder.split(separator)
for ty in types:
hero.say(ty)
# There are the same number of peasants as types.
for index in range(len(peasants)):
peasant = peasants[index]
x = 16
y = 10 + index * 12
# Get buildType by index from the array of types:
buildType = types[index]
# Command the peasant to buildXY the buildType at x and y:
if peasant.type=="peasant":

hero.command(peasant, buildType, {"x":x,"y":y})

# Watch for enemies and move peasants when ogres attack.
while True:
enemy = hero.findNearestEnemy()
if enemy:
hideUnits(peasants)
break

# Fight the ogres:
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)

``````

It is failing with it does not have `palisade`. How do I get it?

Thanks

3 Likes

I havenâ€™t done that level, but after looking at your code I was wondering if maybe whatâ€™s happening is that the peasants start building when you call the `for-loop`, and then the code proceeds into your first `while-loop`, which has your hero disperse them if an enemy shows up, but never calls them back.

(I also donâ€™t think it should be terminating, so I donâ€™t think youâ€™re even getting to the second `while-loop` - is your hero attacking the enemies? If heâ€™s not, then this may be the problem.)

If Iâ€™m right, the basic story is that you need a way to get your hero to attack in the same loop, and then, when there arenâ€™t any enemies, to command the peasants to get back in place.

Hope that helps!

3 Likes

The problem is in the command build itâ€™s supposed to be formatted like

``````hero.command(peasant, "build", "whatever type needed to build", x, y)
``````

NOT

``````hero.command(peasant, "whatever type needed to build", {"x":x, "y":y})
``````
2 Likes

Its actually:

``````hero.command(peasant, "buildXY", "whatever type needed to build", x, y)
``````
3 Likes