# 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","soldier","archer"]
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
solider = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold > hero.costOf(summonTypes["soldier"]):
hero.summon(summonTypes[soldier])
#type = summonTypes[???]
def Item():
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x,item.pos.y)
continue
def Attack():
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
while True:
summonTroops()
Item()
Attack()
the error is on this line
if hero.gold > hero.costOf(summonTypes["soldier"]):
here is a screen shot of what the error says
thanks @Deadpool198
What @AnSeDra is saying, is that when you use call summonTypes, you need a value for the index here:
summonTypes[Value Here]
When you call summonTypes using a “soldier” as the index, it will give you an error because “soldier” is not an index.
Hope this isn’t too confusing and helps you out!
error says true is not defined here is my current code
# 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","soldier","archer","soldier","soldier"]
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
solider = summonTypes[len(hero.built) % len(summonTypes)]
while true:
for i in range(len(summonTypes)):
if hero.gold >= hero.costOf(summonTypes[i]):
hero.summon(summonTypes[i])
#type = summonTypes[???]
def Item():
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x,item.pos.y)
continue
def Attack():
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
while True:
summonTroops()
Item()
Attack()
@098765432123, sorry I just realized that the code I gave actually has an error. For the summonTroops() function, you want to take out that while loop because you are already running summonTroops() in your while loop below. (It will not work because your hero will just try to summon forever)
so my hero now collects coins and summons troops but the hero attacks the enemy then collects a coin then attacks again here is the code:
# 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","soldier","archer","soldier","soldier"]
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
solider = summonTypes[len(hero.built) % len(summonTypes)]
for i in range(len(summonTypes)):
if hero.gold >= hero.costOf(summonTypes[i]):
hero.summon(summonTypes[i])
#type = summonTypes[???]
def Item():
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x,item.pos.y)
def Attack():
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
while True:
summonTroops()
Item()
Attack()
your code works fine with mine maybe because my equipment can withstand the thing but the soldiers are supposed to attack the witches and your hero is supposed to attack the enemies