Here is my code dont know why its wrong
# 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", "soldier", "archer", "soldier", "archer"]
enemies = hero.findEnemies()
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
type = summonTypes[len(hero.built) % len(summonTypes)]
coin = hero.findNearest(hero.findItems())
if hero.gold >= hero.costOf(type):
hero.summon(type)
hero.findNearestItem()
hero.move(coin.pos)
def commandTroops():
friends = hero.findFriends()
for friend in friends:
if friend != "palisade":
enemies=hero.findEnemies()
enemy = hero.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
while True:
summonTroops()
commandTroops()
This one may take a bit of working through. To start, letās work on your summonTroops function. Is there a reason why you are combining summoning with collecting gold? It might be better to have a separate function dedicated to just collection. You can call this new function from your while loop, providing the conditions are rightā¦(hint implied herein
)
1 Like
Donāt forget to add the .type
when dealing types.
Try using friend.type != "palisade"
1 Like
This is my code now, still my hero moves a bit and then he stops
# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer", "soldier", "archer", "soldier", "archer"]
enemies = hero.findEnemies()
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
type = summonTypes[len(hero.built) % len(summonTypes)]
coin = hero.findNearest(hero.findItems())
if hero.gold >= hero.costOf(type):
hero.summon(type)
hero.findNearestItem()
hero.move(coin.pos)
def commandTroops():
friends = hero.findFriends()
for friend in friends:
if friend.type != "palisade":
enemies=hero.findEnemies()
enemy = hero.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
while True:
summonTroops()
commandTroops()
This is my code now, the hero dosent summon the troops but it collects the gold
# 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", "soldier", "archer", "soldier", "archer"]
enemies = hero.findEnemies()
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)
while True:
coin = hero.findNearestItem()
hero.findNearestItem()
hero.move(coin.pos)
def commandTroops():
friends = hero.findFriends()
for friend in friends:
if friend.type != "palisade":
enemies=hero.findEnemies()
enemy = hero.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
while True:
summonTroops()
commandTroops()
Your first while True loop is going to run foreverā¦the second one will never start. Instead, rename the first one to ādef findItems():ā (or something like that), making it a function definition.
Then, you can call it from your actual while true loop, just like you are doing with summonTroops and commandTroops.
1 Like
This is my code now
# 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", "soldier", "archer", "soldier", "archer"]
enemies = hero.findEnemies()
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)
def finditems():
coin = hero.findNearestItem()
hero.findNearestItem()
hero.move(coin.pos)
def commandTroops():
friends = hero.findFriends()
for friend in friends:
if friend.type != "palisade":
enemies=hero.findEnemies()
enemy = hero.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
while True:
summonTroops()
commandTroops(
the code dosent have any errors but my hero dosent move
can you put your successful code down be cause i keep having problems with mine
well also in this discourse we dont post solutions because if you just copy someone elses code then you dont learn
Yes, as @cheddarcheese said, we donāt give solutions because the main goal of this forum is to help, not give answers.
If you tried, and post your code here correctly as it says here, weāll be more than willing to help you!
Lydia
kay so here is my code what is wrong
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ā, āsoldierā, āarcherā, āsoldierā, āarcherā]
enemies = hero.findEnemies()
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)
def finditems():
coin = hero.findNearestItem()
hero.findNearestItem()
hero.move(coin.pos)
def commandTroops():
friends = hero.findFriends()
for friend in friends:
if friend.type != āpalisadeā:
enemies=hero.findEnemies()
enemy = hero.findNearestEnemy()
if enemy:
hero.command(friend, āattackā, enemy)
while True:
finditems()
summonTroops()
commandTroops()
Can you post your code correctly as it says here ?
Thanks!
Lydia