# Medical Attention - Python

**URL:** <https://discourse.codecombat.com/t/medical-attention-python/25590>\
**Category:** Level Help\
**Created:** [November 20, 2020, 6:45pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590 "2020-11-20T18:45:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlwaysConfused](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alwaysconfused/32/20546_2.png) [@AlwaysConfused](https://discourse.codecombat.com/u/AlwaysConfused)\
**Post date:** [November 20, 2020, 6:45pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/1 "2020-11-20T18:45:43Z")

</div>

The only error is that my attack code gets null, even though `enemy = hero.findNearestEnemy` was directly above it. Heres my code.

```python
# Ask the healer for help when you're under one-half health.

while True:
    currentHealth = hero.health
    healingThreshold = hero.maxHealth / 2
    # If your current health is less than the threshold,
    # move to the healing point and say, "heal me".
    # Otherwise, attack. You'll need to fight hard!
    if currentHealth < healingThreshold:
        hero.moveXY(65, 46)
        hero.say("Heal, please!")
    elif currentHealth > healingThreshold:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)

```

---

<div class="post-metadata">

**Author:** ![AlwaysConfused](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alwaysconfused/32/20546_2.png) [@AlwaysConfused](https://discourse.codecombat.com/u/AlwaysConfused)\
**Post date:** [November 20, 2020, 6:48pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/2 "2020-11-20T18:48:08Z")

</div>

The hints are also completely useless.

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [November 20, 2020, 6:54pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/3 "2020-11-20T18:54:58Z")

</div>

hey @AlwaysConfused tip don’t write anything the soldiers will do it

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [November 20, 2020, 6:56pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/4 "2020-11-20T18:56:00Z")

</div>

You need an if statement before the attack statement. Your hero can only attack an enemy if there is an enemy.

---

<div class="post-metadata">

**Author:** ![AlwaysConfused](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alwaysconfused/32/20546_2.png) [@AlwaysConfused](https://discourse.codecombat.com/u/AlwaysConfused)\
**Post date:** [November 20, 2020, 6:56pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/5 "2020-11-20T18:56:06Z")

</div>

um  
if i do then i die.

---

<div class="post-metadata">

**Author:** ![AlwaysConfused](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alwaysconfused/32/20546_2.png) [@AlwaysConfused](https://discourse.codecombat.com/u/AlwaysConfused)\
**Post date:** [November 20, 2020, 6:59pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/6 "2020-11-20T18:59:06Z")

</div>

Ok, but the error is there but my code is good. The error looks like this. I absolutely hate this error by the way.

 ![error of death](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/2/8/28a9d2db928da642d13358be895e0059df98e483.jpeg)

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [November 20, 2020, 7:10pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/7 "2020-11-20T19:10:11Z")

</div>

> [@AlwaysConfused](#):
>
> ```python
> elif currentHealth > healingThreshold:
> enemy = hero.findNearestEnemy()
> hero.attack(enemy)
> 
> ```

You defined enemy (that’s good)  
Then you need to check if there is an enemy  
If there is an enemy, attack it.  
Lydia

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [November 20, 2020, 7:20pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/8 "2020-11-20T19:20:32Z")

</div>

hmm weird I succeeded without writing any code

---

<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:** [November 20, 2020, 7:21pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/9 "2020-11-20T19:21:59Z")

</div>

Well, that’s not really the way you’re supposed to do the level. You don’t really learn anything from that.

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [November 20, 2020, 7:23pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/10 "2020-11-20T19:23:11Z")

</div>

I do it all the time @Deadpool198 take advantage of the level

---

<div class="post-metadata">

**Author:** ![AlwaysConfused](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alwaysconfused/32/20546_2.png) [@AlwaysConfused](https://discourse.codecombat.com/u/AlwaysConfused)\
**Post date:** [November 20, 2020, 7:23pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/11 "2020-11-20T19:23:50Z")

</div>

Ok cool, but HOW do i find it? `enemy = hero.findNearestEnemy` is the only way!

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [November 20, 2020, 7:27pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/12 "2020-11-20T19:27:54Z")

</div>

You already found the enemy, now you need to check if there is an enemy.  
Lydia

---

<div class="post-metadata">

**Author:** ![cheddarcheese](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cheddarcheese/32/22417_2.png) [@cheddarcheese](https://discourse.codecombat.com/u/cheddarcheese)\
**Post date:** [November 20, 2020, 7:29pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/13 "2020-11-20T19:29:41Z")

</div>

like this:

```python
if enemy:
    hero.attack(enemy)
    enemy = hero.findNearestEnemy()

```

but put it in a while true loop

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [November 20, 2020, 7:43pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/14 "2020-11-20T19:43:34Z")

</div>

You need find the enemy, which you have done, but you can only attack it if it exists, so you should do an `if enemy:`, and then have your attack statement inside of the if loop. The reason for if statements is that there is not always that something, in this case there is not always an enemy for your hero to attack.

---

<div class="post-metadata">

**Author:** ![AlwaysConfused](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alwaysconfused/32/20546_2.png) [@AlwaysConfused](https://discourse.codecombat.com/u/AlwaysConfused)\
**Post date:** [November 23, 2020, 6:19pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/15 "2020-11-23T18:19:36Z")

</div>

It says there IS no enemy.

---

<div class="post-metadata">

**Author:** ![AlwaysConfused](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alwaysconfused/32/20546_2.png) [@AlwaysConfused](https://discourse.codecombat.com/u/AlwaysConfused)\
**Post date:** [November 23, 2020, 6:19pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/16 "2020-11-23T18:19:51Z")

</div>

Already in one. (20)

---

<div class="post-metadata">

**Author:** ![AlwaysConfused](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alwaysconfused/32/20546_2.png) [@AlwaysConfused](https://discourse.codecombat.com/u/AlwaysConfused)\
**Post date:** [November 23, 2020, 6:20pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/17 "2020-11-23T18:20:17Z")

</div>

Thanks i might try that

---

<div class="post-metadata">

**Author:** ![AlwaysConfused](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alwaysconfused/32/20546_2.png) [@AlwaysConfused](https://discourse.codecombat.com/u/AlwaysConfused)\
**Post date:** [November 23, 2020, 6:25pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/18 "2020-11-23T18:25:35Z")

</div>

Apparently I won the game by doing absolutely nothing. I seemed to have healed a little, but my code broke somehow. Gotta fix that.

---

<div class="post-metadata">

**Author:** ![AlwaysConfused](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alwaysconfused/32/20546_2.png) [@AlwaysConfused](https://discourse.codecombat.com/u/AlwaysConfused)\
**Post date:** [November 23, 2020, 6:26pm UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/19 "2020-11-23T18:26:40Z")

</div>

Code:

```python
# Ask the healer for help when you're under one-half health.
enemy = hero.findNearestEnemy()
while True:
    currentHealth = hero.health
    healingThreshold = hero.maxHealth / 2
    # If your current health is less than the threshold,
    # move to the healing point and say, "heal me".
    # Otherwise, attack. You'll need to fight hard!
    if currentHealth < healingThreshold:
        hero.moveXY(65, 46)
        hero.say("Heal, please!")
    else:
        if enemy:
            hero.attack(enemy)

```

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [November 24, 2020, 8:29am UTC](https://discourse.codecombat.com/t/medical-attention-python/25590/20 "2020-11-24T08:29:23Z")

</div>

Hi AlwaysConfused,

At the moment you find an enemy at the start (outside the while True loop), but once you’ve killed that enemy you don’t find another enemy. What line of code do you need to move to do this?

Jenny

[Next page](https://discourse.codecombat.com/t/medical-attention-python/25590.md?page=2)
