I added soldier = soldiers[i]
to the loop before hero.command(soldier, "defend", { "x": 98, "y": 28 })
but I got this:
So, your minion argument is receiving null. Somehow, your hero.command
gets None
instead of a soldier to command. How’s None
getting stored into soldier
?
for i in range(len(defend)):
soldier = soldiers[i]
...
Just focus on these lines.
Compare the length of defend
to the length of soldiers
.
My post from earlier
Near the top of your program, you have already defined two positions inside the defend
array.
Perhaps that is the root cause of this issue.
how would I do this exactly? I am only 10 years old I do not understand what you mean!!
# there are 5 items in this list:
fiveItems = [10, 20, 30, 40, 50]
# there are 3 items in this list:
threeItems = [71, 72, 73]
for i in range(len(fiveItems)): # this for loop iterates 5 times (i counts from 0 to 4),
# because there are 5 items in fiveItems
currentItem = threeItems[i] # what happens when i is 3 or 4?
hero.say(currentItem)
hero.wait(1)
why does it do that? why not count up to 5?
It’s how loops work in python. range(5)
counts from 0 to 4:
0, 1, 2, 3, 4
Notice that there are 5 numbers.
Items in a list in python are zero-based. That means, the first item has an index of 0.
items = ["first", "second", "third", "fourth", "fifth"]
hero.say(items[0]) # hero says "first"
hero.say(items[1]) # hero says "second"
hero.say(items[2]) # hero says "third"
hero.say(items[3]) # hero says "fourth"
hero.say(items[4]) # hero says "fifth"
so what does this mean?
for i in range(len(defend))
len(defend)
will give you how many items there are in defend
.
range(len(defend))
gives you a range of numbers from 0
up to one less than the amount of items there are in defend
, counting up by 1
each time.
So, if there are 5 items in defend
, then the range will give you 0, 1, 2, 3, 4
so does that mean that the for loop only commands soldiers 1, 2, 3, 4 and 5? (There are 6 soldiers)
The for loop will command every soldier if it’s:
for i in range(len(soldiers)):
But the latest version of your posted code is:
for i in range(len(defend)):
so, I change for i in range(len(defend)):
to for i in range(len(soldiers)):
?
well, i did it but…
All of your soldiers seem to be commanded to defend the same position.
changed it to
for i in range(len(soldiers)):
soldier = soldiers[i]
target = defend[i]
hero.command(soldier, "defend", target)
so now their all spread out
but this happened:
I really can’t help you further, since I don’t even have access to the level.
oh… I’m sorry. I did now know you were not a subscriber
Try watching the video guide
But the vedio guide does not work!
Can you see where it is?