# Ice Hunter with soldiers \[Python\]

**URL:** <https://discourse.codecombat.com/t/ice-hunter-with-soldiers-python/27599>\
**Category:** français (French)\
**Created:** [February 24, 2021, 9:49am UTC](https://discourse.codecombat.com/t/ice-hunter-with-soldiers-python/27599 "2021-02-24T09:49:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nico](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nico/32/20884_2.png) [@Nico](https://discourse.codecombat.com/u/Nico)\
**Post date:** [February 24, 2021, 9:49am UTC](https://discourse.codecombat.com/t/ice-hunter-with-soldiers-python/27599/1 "2021-02-24T09:49:37Z")

</div>

Hi,  
I wanted to solve this level by summoning soldiers but I got an error message when the first yak die.  
TypeError Need a object  
for the ligne : hero.attack(enemy)

If you have any insight I would be glad

🙂

```python
# Hunt for 4 yaks. Choose only the small ones.
# Small yak names contain a "bos" substring.

# This function checks if a word contains a substring.
def isSubstring(word, substring):
    # We iterate through the start indexes only.
    rightEdge = len(word) - len(substring)
    # Loop through the indexes of the word.
    for i in range(rightEdge + 1):
        # For each of them loop through the substring
        for j in range(len(substring)):
            # Use an offset for the word's indexes.
            shiftedIndex = i + j
            # If letters aren't the same:
            if word[shiftedIndex] != substring[j]:
                # Check the next start index in the word.
                break
            # If it was the last letter in the substring:
            if j == len(substring) - 1:
                # Then the substring is in the word.
                return True
    # We haven't found the substring in the word.
    return False

# Loop through all enemies.
enemies = hero.findEnemies()
for e in range(len(enemies)):
    enemy = enemies[e]
    # Use the function isSubstring to check
    # if an enemy.id contains "bos":
    if isSubstring(enemy.id, "bos"):
        # Then defeat it.
        while enemy.health > 0:
            while hero.costOf("soldier") < hero.gold:
                hero.summon("soldier")
                soldiers = hero.findFriends()
                for i in len(soldiers):    
                    hero.command(soldiers[i], "attack", enemy)
            hero.attack(enemy)

```

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [February 24, 2021, 10:41am UTC](https://discourse.codecombat.com/t/ice-hunter-with-soldiers-python/27599/2 "2021-02-24T10:41:45Z")

</div>

Bonjour @Nico, et bienvenue au forum CodeCombat!  
La problem est sur cette ligne:

> [@Nico](#):
>
> `for i in len(soldiers):`

Ceci n’est pas exactement comment le for loop fonctionne. On doit utiliser range() aussi, comme dans l’autre for loop.  
Par exemple:

```python
for x in range(len(enemies)):

```

Mais avec friends dans le place d’enemies.  
J’espère que ça vous aidez.  
Danny

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [February 25, 2021, 7:50am UTC](https://discourse.codecombat.com/t/ice-hunter-with-soldiers-python/27599/3 "2021-02-25T07:50:17Z")

</div>

Closed on February 25, 2021, 7:50am UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
