 # Binary Deployment Error Message

What is wrong with my code?

``````# Recruit soldiers and archers to fill out each squadron.
# Each paladin has a decimal number stored in her deployment attribute.
# Convert these to binary and represent them with lines of soldiers and archers next to each paladin.
# Soldiers are 0s, archers are 1s.
# For the bonus goal, add griffins as 2s for trinary number lines next to the warlocks.
# Check the guide for help with binary numbers.
def binary(number):
string = ""
if number > 128:
number = number - 128
string = string +"1"
else:
string = string +"0"
if number > 64:
number = number - 64
string = string +"1"
else:
string = string + "0"
if number > 32:
number = number - 32
string = string + "1"
else:
string = string + "0"
if number > 16:
number = number - 16
string = string + "1"
else:
string = string + "0"
if number > 8:
number = number - 8
string = string + "1"
else:
string = string + "0"
if number > 4:
number = number - 4
string = string + "1"
else:
string = string + "0"
if number > 2:
number = number - 2
string = string + "1"
else:
string = string + "0"
if number > 1:
number = number - 1
string = string + "1"
else:
string = string + "0"
return string

while True:

binaryNum = binary(number+1)
for j in range(len(binaryNum)):
n = binaryNum[j]
sol = hero.findByType("soldier")
arc = hero.findByType("archer")
nearestSol = hero.findNearest(sol)
nearestArc = hero.findNearest(arc)
if n == "0":
hero.summon("soldier")
hero.command(nearestSol, "move", {'x':14+2*j, 'y':58-7*i})
elif n == "1":
hero.summon("archer")
hero.command(nearestArc, "move", {'x':14+2*j,'y':58-7*i})

``````

I get the error hero placeholder needs something to command.

1 Like

A unit only exists after it is summoned. At the moment, you’re finding `nearestSol` before summoning the soldier, so of course it can’t be assigned to the newly summoned soldier. The same is true with archers.

One option is to move `hero.findNearest(hero.findByType(...)))` after the summon command. Another is to instead reference the latest unit by finding the last unit in `hero.built`.

I tried it, but it doesn’t work. I still get the same error message
Code:

``````# Recruit soldiers and archers to fill out each squadron.
# Each paladin has a decimal number stored in her deployment attribute.
# Convert these to binary and represent them with lines of soldiers and archers next to each paladin.
# Soldiers are 0s, archers are 1s.
# For the bonus goal, add griffins as 2s for trinary number lines next to the warlocks.
# Check the guide for help with binary numbers.
def binary(number):
string = ""
if number > 128:
number = number - 128
string = string +"1"
else:
string = string +"0"
if number > 64:
number = number - 64
string = string +"1"
else:
string = string + "0"
if number > 32:
number = number - 32
string = string + "1"
else:
string = string + "0"
if number > 16:
number = number - 16
string = string + "1"
else:
string = string + "0"
if number > 8:
number = number - 8
string = string + "1"
else:
string = string + "0"
if number > 4:
number = number - 4
string = string + "1"
else:
string = string + "0"
if number > 2:
number = number - 2
string = string + "1"
else:
string = string + "0"
if number > 1:
number = number - 1
string = string + "1"
else:
string = string + "0"
return string

while True:

binaryNum = binary(number+1)
for j in range(len(binaryNum)):
n = binaryNum[j]
sol = hero.findByType("soldier")
arc = hero.findByType("archer")
if n == "0":
hero.summon("soldier")
nearestSol = hero.findNearest(sol)
hero.command(nearestSol, "move", {'x':14+2*j, 'y':58-7*i})
elif n == "1":
hero.summon("archer")
nearestArc = hero.findNearest(arc)
hero.command(nearestArc, "move", {'x':14+2*j,'y':58-7*i})

``````

Never mind. Here’s my new code

``````# Recruit soldiers and archers to fill out each squadron.
# Each paladin has a decimal number stored in her deployment attribute.
# Convert these to binary and represent them with lines of soldiers and archers next to each paladin.
# Soldiers are 0s, archers are 1s.
# For the bonus goal, add griffins as 2s for trinary number lines next to the warlocks.
# Check the guide for help with binary numbers.
def binary(number):
string = ""
if number > 128:
number = number - 128
string = string +"1"
else:
string = string +"0"
if number > 64:
number = number - 64
string = string +"1"
else:
string = string + "0"
if number > 32:
number = number - 32
string = string + "1"
else:
string = string + "0"
if number > 16:
number = number - 16
string = string + "1"
else:
string = string + "0"
if number > 8:
number = number - 8
string = string + "1"
else:
string = string + "0"
if number > 4:
number = number - 4
string = string + "1"
else:
string = string + "0"
if number > 2:
number = number - 2
string = string + "1"
else:
string = string + "0"
if number > 1:
number = number - 1
string = string + "1"
else:
string = string + "0"
return string

while True:

binaryNum = binary(number+1)
for j in range(len(binaryNum)):
n = binaryNum[j]
sol = hero.findByType("soldier")
arc = hero.findByType("archer")
if n == "0":
hero.summon("soldier")
nearestSol = hero.findNearest(sol)
if nearestSol:

hero.command(nearestSol, "move", {'x':14+8*j, 'y':58-8*i})
elif n == "1":
hero.summon("archer")
nearestArc = hero.findNearest(arc)
if nearestArc:

hero.command(nearestArc, "move", {'x':14+8*j,'y':58-8*i})

``````

but it still doesn’t work. There is no error message but some troops
get to the wrong spot

Sure, since `sol = hero.findByType("soldier")` is the list of all soldiers before the one you just summoned.

I changed the code but the same thing happens.
Code:

``````# Each paladin has a decimal number stored in her deployment attribute.
# Convert these to binary and represent them with lines of soldiers and archers next to each paladin.
# Soldiers are 0s, archers are 1s.
# For the bonus goal, add griffins as 2s for trinary number lines next to the warlocks.
# Check the guide for help with binary numbers.
def binary(number):
string = ""
if number > 128:
number = number - 128
string = string +"1"
else:
string = string +"0"
if number > 64:
number = number - 64
string = string +"1"
else:
string = string + "0"
if number > 32:
number = number - 32
string = string + "1"
else:
string = string + "0"
if number > 16:
number = number - 16
string = string + "1"
else:
string = string + "0"
if number > 8:
number = number - 8
string = string + "1"
else:
string = string + "0"
if number > 4:
number = number - 4
string = string + "1"
else:
string = string + "0"
if number > 2:
number = number - 2
string = string + "1"
else:
string = string + "0"
if number > 1:
number = number - 1
string = string + "1"
else:
string = string + "0"
return string

for i in range(5):
binaryNum = binary(number+1)
for j in range(8):
n = binaryNum[j]
if n == "0":
hero.summon("soldier")
sol = hero.findByType("soldier")
nearestSol = hero.findNearest(sol)
if nearestSol:

hero.command(nearestSol, "move", {'x':14+8*j, 'y':58-8*i})
elif n == "1":
hero.summon("archer")
arc = hero.findByType("archer")
nearestArc = hero.findNearest(arc)
if nearestArc:

hero.command(nearestArc, "move", {'x':14+8*j,'y':58-8*i})

``````

Is the nearest unit the newest one?

I don’t know.(20 chars)

Not necessarily, there’s no good reason for them to be correlated. Try referencing the last built unit, which is the last element in `hero.built`

Thanks for your help. It worked.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.