# Practice using modulo to loop over an array
# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer"]
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
#type = summonTypes[???]
soldier = summonTypes["soldier"]
archer = summonTypes["archer"]
len(hero.built) % len(summonTypes)
def collectCoins():
coin = hero.findNearestItem()
if coin:
hero.move(coin.pos)
def commandTroops():
enemy = hero.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
hero.command(soldier, "attack", enemy)
else:
hero.command(soldier, "defend", hero.pos)
hero.command(archer, "defend", hero.pos)
while True:
summonTroops()
collectCoins()
commandTroops()
Ok, Iâm afraid to say your code still has the same problems it had before.
Let me explain this in more detail.
Firstly the summonTroops() function:
You need to remember, this function is for summoning only. Nothing else needs to happen here (like defining soldier and archer).
You had it right when you put this in:
type = summonTypes[]
Now you need to combine that line with this one:
len(hero.built) % len(summonTypes)
You need to put that line in the [] of the first line, like it says here:
Where the question marks are.
Once youâve defined âtypeâ, you need to summon it. (use it like âsoldierâ or âarcherâ, but without quotes because the value the type variable is carrying is a string already.
if hero.gold >= ......
hero.summon.....
Now onto commandTroops():
Start this function from scratch.
As I recommended before, why donât you use a for loop? (it really wonât work without one.)
You should probably look at previous mountain levels to explain more about these topics, because you seem a little confused about it.
# you could do something like this:
1) make a friends array
2) make a for loop to loop through that array
3) because you seem to want your archers and soldiers to do the same
thing you could just command the friend to do whatever you want them both to do.
i now need help i am stuck and this is what i have
for friend in friends:
# Practice using modulo to loop over an array
# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer"]
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
#type = summonTypes[???]
soldier = summonTypes["soldier"]
archer = summonTypes["archer"]
len(hero.built) % len(summonTypes)
def collectCoins():
coin = hero.findNearestItem()
if coin:
hero.move(coin.pos)
def commandTroops():
enemy = hero.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
hero.command(soldier, "attack", enemy)
else:
hero.command(soldier, "defend", hero.pos)
hero.command(archer, "defend", hero.pos)
while True:
summonTroops()
collectCoins()
commandTroops()
hero.findFriends()
for friend in friends:
# Practice using modulo to loop over an array
# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer"]
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
#type = summonTypes[???]
soldier = summonTypes["soldier"]
archer = summonTypes["archer"]
len(hero.built) % len(summonTypes)
def collectCoins():
coin = hero.findNearestItem()
if coin:
hero.move(coin.pos)
def commandTroops():
enemy = hero.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
hero.command(soldier, "attack", enemy)
else:
hero.command(soldier, "defend", hero.pos)
hero.command(archer, "defend", hero.pos)
while True:
summonTroops()
collectCoins()
commandTroops()
friends = hero.findFriends()
for friend in friends:
# Practice using modulo to loop over an array
# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer"]
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
#type = summonTypes[???]
soldier = summonTypes["soldier"]
archer = summonTypes["archer"]
len(hero.built) % len(summonTypes)
def collectCoins():
coin = hero.findNearestItem()
if coin:
hero.move(coin.pos)
def commandTroops():
enemy = hero.findNearestEnemy()
if enemy:
hero.summon(soldier, "attack", enemy)
hero.summon(soldier, "attack", enemy)
else:
hero.summon(soldier, "defend", hero.pos)
hero.summon(archer, "defend", hero.pos)
while True:
summonTroops()
collectCoins()
commandTroops()
Hi @ZAX155,
I think youâre code needs a bit of⌠restructuring.
Letâs look at this on a large scale, because youâre problem is on a large scale (the error message is the least of your worries Iâm afraid)
The overall structure of your code at the moment is:
for friend in friends:
summonTypes
def summonTroops():
def collectCoins():
def commandTroops():
while True:
# call functions
Why are you defining the functions and running the while True loop inside a for loop? You donât need to run all that code for each friend, you only want to run the command code for each friend.
What Iâm trying to say is: when you command your friends- use a for loop! Every time, itâs simply the best option. (you will have to put it in the commandTroops function)
Also please could you limit how many posts you make, you could edit the original post to change your code rather than making multiple long posts.
Thanks, I hope this helps
Danny