# Desert, sarven-treasure, problème de code

**URL:** https://discourse.codecombat.com/t/desert-sarven-treasure-probleme-de-code/14971
**Category:** français (French)
**Created:** [June 14, 2018, 3:50pm UTC](https://discourse.codecombat.com/t/desert-sarven-treasure-probleme-de-code/14971 "2018-06-14T15:50:50Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![truffeb](https://avatars.discourse-cdn.com/v4/letter/t/3be4f8/32.png) [@truffeb](https://discourse.codecombat.com/u/truffeb)
#### Post date: [June 14, 2018, 3:50pm UTC](https://discourse.codecombat.com/t/desert-sarven-treasure-probleme-de-code/14971/1 "2018-06-14T15:50:50Z")

</div>

Bonjour,  
Pouvez-vous m’aider à corriger ce code, j’ai une boucle infinie mais je ne comprends pas pourquoi.  
J’utilise comme héro Okar.

```python
def defEnemyAttack():
    enemies = hero.findEnemies()
    faible = None
    moinsSante >= 70
    enemyIndex = 0
        
    while enemyIndex < len (enemies):
        enemy = enemies [enemyIndex]
        if enemy.health < moinsSante:
            faible = enemy
            moinsSante = enemy.health
                        
            hero.attack(enemy)
        elif enemy.health > moinsSante:
            fort = enemy
            if hero.canCast("chain-lightning", fort):
                hero.cast("chain-lightning", fort)
            elif hero.isReady("bash"):
                hero.bash(fort)
            elif hero.isReady("throw") and hero.distanceTo(fort.pos) < hero.throwRange:
                hero.throw(fort)    
            elif hero.isReady("stomp"):
                hero.stomp(fort)
            elif hero.isReady("hurl"):
                hero.hurl(fort)
            else:
                hero.attack(enemy)
                        
            enemyIndex += 1

   

def gold():
    presGold = None
    minGoldDist = 9001
    coinIndex =0
    coins = hero.findItems()
    
    for coin in coins:
        dist = hero.distanceTo(coin)/coin.value
        if minGoldDist > dist:
            presGold =coin
            minGoldDist = dist
            hero.move(presGold.pos)

def point2():
    points=[{"x":5, "y":49},{"x":5, "y":20},{"x":76, "y":51},{"x":76, "y":19}] 
    pointspres = None
    minpointdis = 9001
    for point in points:
        distance2 = hero.distanceTo(point)
        if minpointdis > distance2:
            pointspres= point
            minpointdis= distance2
            hero.move(pointspres)

while True:
    item = hero.findNearestItem()
    enemy = hero.findNearestEnemy()
    
    if item :
        gold()
    
    if enemy:
        distance = hero.distanceTo(enemy)
        if distance<20:
            defEnemyAttack()
            if hero.health/3:
                point2()
                gold()
            

```

---

<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: [June 14, 2018, 5:06pm UTC](https://discourse.codecombat.com/t/desert-sarven-treasure-probleme-de-code/14971/2 "2018-06-14T17:06:24Z")

</div>

C’est parce que tu as

> [@truffeb](#):
>
> moinsSante \>= 70

ils devrait etre `moinsSante = 70` sans le “\>”.  
J’espère que cela vous aide! 😄  
( désolé pour mon mauvais français, je suis en train d’apprendre. )

---

<div class="post-metadata">

### Author: ![truffeb](https://avatars.discourse-cdn.com/v4/letter/t/3be4f8/32.png) [@truffeb](https://discourse.codecombat.com/u/truffeb)
#### Post date: [June 14, 2018, 5:34pm UTC](https://discourse.codecombat.com/t/desert-sarven-treasure-probleme-de-code/14971/3 "2018-06-14T17:34:02Z")

</div>

Ton français est bon 😀  
Mon personnage ramasse les items puis combat le premier ogre, le deuxième ogre a 7 de vie et le héro ne bouge plus. Le temps est ensuite écoulé.

---

<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: [June 14, 2018, 5:52pm UTC](https://discourse.codecombat.com/t/desert-sarven-treasure-probleme-de-code/14971/4 "2018-06-14T17:52:57Z")

</div>

Ils devrait etre metre

> [@truffeb](#):
>
> enemyIndex += 1

quatre espaces en arriere. ( pardon pour les accents je ne peux pas les mettre en PC )

---

<div class="post-metadata">

### Author: ![truffeb](https://avatars.discourse-cdn.com/v4/letter/t/3be4f8/32.png) [@truffeb](https://discourse.codecombat.com/u/truffeb)
#### Post date: [June 14, 2018, 6:01pm UTC](https://discourse.codecombat.com/t/desert-sarven-treasure-probleme-de-code/14971/5 "2018-06-14T18:01:23Z")

</div>

Merci 😀  
Il fini sont combat, il commence à ramasser les pièces puis hésites ou il doit aller. J’ai ajouté coinIndex +=1 mais cela ne change rien 😥

---

<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: [June 15, 2018, 9:40am UTC](https://discourse.codecombat.com/t/desert-sarven-treasure-probleme-de-code/14971/6 "2018-06-15T09:40:17Z")

</div>

Oh, okeydokey 🤔, je crois que il faut metre

> [@truffeb](#):
>
> ```python
> if hero.canCast("chain-lightning", fort): 
> hero.cast("chain-lightning", fort) 
> elif hero.isReady("bash"): 
> hero.bash(fort) 
> elif hero.isReady("throw") and hero.distanceTo(fort.pos) &lt; hero.throwRange: 
> hero.throw(fort) 
> elif hero.isReady("stomp"): 
> hero.stomp(fort) 
> elif hero.isReady("hurl"): 
> hero.hurl(fort) 
> else: 
> hero.attack(enemy)
> 
> ```

et

> [@truffeb](#):
>
> `hero.attack(enemy)`

(ligne 13)

comme ca:

```python
def defEnemyAttack():
    enemies = hero.findEnemies()
    faible = None
    moinsSante = 70
    enemyIndex = 0
        
    while enemyIndex < len (enemies):
        enemy = enemies [enemyIndex]
        if enemy.health < moinsSante:
            faible = enemy
            moinsSante = enemy.health
        elif enemy.health > moinsSante:
            fort = enemy             
        enemyIndex += 1

    if faible:
        hero.attack(faible)
    if fort:
        if hero.canCast("chain-lightning", fort):
            hero.cast("chain-lightning", fort)
        elif hero.isReady("bash"):
            hero.bash(fort)
        elif hero.isReady("throw") and hero.distanceTo(fort.pos) < hero.throwRange:
            hero.throw(fort)    
        elif hero.isReady("stomp"):
            hero.stomp(fort)
        elif hero.isReady("hurl"):
            hero.hurl(fort)
        else:
            hero.attack(enemy)
   

def gold():
    presGold = None
    minGoldDist = 9001
    coinIndex =0
    coins = hero.findItems()
    
    for coin in coins:
        dist = hero.distanceTo(coin)/coin.value
        if minGoldDist > dist:
            presGold =coin
            minGoldDist = dist
            hero.move(presGold.pos)

def point2():
    points=[{"x":5, "y":49},{"x":5, "y":20},{"x":76, "y":51},{"x":76, "y":19}] 
    pointspres = None
    minpointdis = 9001
    for point in points:
        distance2 = hero.distanceTo(point)
        if minpointdis > distance2:
            pointspres= point
            minpointdis= distance2
            hero.move(pointspres)

while True:
    item = hero.findNearestItem()
    enemy = hero.findNearestEnemy()
    
    if item :
        gold()
    
    if enemy:
        distance = hero.distanceTo(enemy)
        if distance<20:
            defEnemyAttack()
            if hero.health/3:
                point2()
                gold()

```

Je pense que cela fonctionne.

---

<div class="post-metadata">

### Author: ![truffeb](https://avatars.discourse-cdn.com/v4/letter/t/3be4f8/32.png) [@truffeb](https://discourse.codecombat.com/u/truffeb)
#### Post date: [June 15, 2018, 9:48am UTC](https://discourse.codecombat.com/t/desert-sarven-treasure-probleme-de-code/14971/7 "2018-06-15T09:48:30Z")

</div>

Merci beaucoup 🤗

---

<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: [June 15, 2018, 2:57pm UTC](https://discourse.codecombat.com/t/desert-sarven-treasure-probleme-de-code/14971/8 "2018-06-15T14:57:53Z")

</div>

Phew! je suis content que ca fonctionne pour toi, s’il vous plaît pourriez-vous mettre [RESOLU] dans le titre et dans le titre de tous les autres problèmes que vous avez résolus aussi.  
Merci! 😉
