[Solved] Mixed unit tactics hlp

hi, here is my 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","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
Screenshot (25)
thanks @Deadpool198

1 Like

Don’t you think that instead of a string, you should use a number for summonTypes? (And btw sorry because I am not @Deadpool198)

Andrei

1 Like

i dont understand what you mean

1 Like

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!

Grzmot

1 Like

so i put the number instead of soldiers?

1 Like

If you want to summon warriors and archers in that order what you want to do is iterate through summonTypes. It should look like this:

while true:
      for i in range(len(summonTypes)):
           if hero.gold >= hero.costOf(summonTypes[i]):
                 hero.summon(summonTypes[i])

Hope this helps!
Grzmot

1 Like

so what do i put in the section? do i put the soldiers still or what?

1 Like

and where do i put the code u gave?

1 Like

If you use that code inside of your summonTroops() function, instead of what you are using right now, I think it should work.

so this happened when i ran the code…

1 Like

what should i do about this?

1 Like

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()

1 Like

In the middle of your summonTroops function you need “while True”, not “while true”.

Jenny

1 Like

now my hero is summoning troops but the hero or the troops dont move

1 Like

The troops are not moving because you are never commanding them.

For this level you don’t actually want to attack. You’re going to want to command your friends to attack while you collect coins.

1 Like

@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)

Grzmot

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()

1 Like

You don’t actually need to attack enemies at all, so you should just remove the attack function.

Grzmot

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