# \[SOLVED\] Doom Glade Little Code Error

**URL:** <https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309>\
**Category:** Level Help\
**Created:** [November 10, 2020, 5:13pm UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309 "2020-11-10T17:13:32Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShiniG](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@ShiniG](https://discourse.codecombat.com/u/ShiniG)\
**Post date:** [November 10, 2020, 5:13pm UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/1 "2020-11-10T17:13:32Z")

</div>

Hi everyone, that’s my first topic, so I’m sorry if I have done something wrong. I’m having an issue with this level, said I have successfully passed. Yet, still have an error with my code.  
Below is my code.

```python
# An ogre army approaches. Use flags to win the battle!
while True:
    enemy = hero.findNearestEnemy()
    if hero.isReady("chain-lightning"):
        hero.cast("chain-lightning", hero.findNearestEnemy())
    elif hero.isReady("drain-life"):
        hero.cast("drain-life", hero.findNearestEnemy())
    elif enemy:
        hero.attack(enemy)

```

And the error message is “Line 5: ArgumentError: **cast** ’s argument **target** should have type **object** , but got **null**. You need something to cast upon.”  
I tried to use enemy variable instead of using findNearestEnemy() all the time, but I got the same error.  
Can someone help me figure out what’s wrong with my code? Big thanks afore.

---

<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 10, 2020, 5:17pm UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/2 "2020-11-10T17:17:09Z")

</div>

please check this topic out @ShiniG[[Essentials] How To Post/Format Your Code Correctly](https://discourse.codecombat.com/t/essentials-how-to-post-format-your-code-correctly/15521)

---

<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 10, 2020, 5:18pm UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/3 "2020-11-10T17:18:13Z")

</div>

and welcome to the discourse

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [November 10, 2020, 5:46pm UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/4 "2020-11-10T17:46:18Z")

</div>

This error happens when your hero is trying to cast a spell on the enemy without verifying if there is an enemy. Eventually you kill all the enemies, yet your code still wants to cast a spell against the nearest enemy. When it can’t find an enemy, you get that error. You always want to check for an enemy before attacking them.

One way to bundle the code together is checking for an enemy before all the different spells and attacks. And when you call the special attacks, use the variable name you used when checking if there is an enemy instead of looking for the enemy in the cast function.

```python
enemy = hero.findNearestEnemy()
if enemy:
   special attack 1 - check to see if ready before attack
   special attack 2 - check to see if ready before attack
   else regular attack

```

---

<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 10, 2020, 6:07pm UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/5 "2020-11-10T18:07:17Z")

</div>

you have to use can cast @ShiniG

---

<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 10, 2020, 6:08pm UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/6 "2020-11-10T18:08:52Z")

</div>

like this hero.canCast(“chain-lightning”, enemy)

---

<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 10, 2020, 6:46pm UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/7 "2020-11-10T18:46:38Z")

</div>

> [@ShiniG](#):
>
> ```python
> if hero.isReady("chain-lightning"):
> hero.cast("chain-lightning", hero.findNearestEnemy())
> elif hero.isReady("drain-life"):
> hero.cast("drain-life", hero.findNearestEnemy())
> 
> ```

The hero can only attack if there is an enemy, you’ve already defined enemy as a variable, so replace `hero.findNearestEnemy()` with enemy.

> [@ShiniG](#):
>
> ```python
> elif enemy:
> hero.attack(enemy)
> 
> ```

Replace the elif with if  
and put this:

> [@ShiniG](#):
>
> ```python
> if hero.isReady("chain-lightning"):
> hero.cast("chain-lightning", hero.findNearestEnemy())
> elif hero.isReady("drain-life"):
> hero.cast("drain-life", hero.findNearestEnemy())
> 
> ```

inside the if enemy loop.  
And put this:

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

As an else loop at the bottom of the if enemy loop.  
Lydia

---

<div class="post-metadata">

**Author:** ![ShiniG](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@ShiniG](https://discourse.codecombat.com/u/ShiniG)\
**Post date:** [November 10, 2020, 11:56pm UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/8 "2020-11-10T23:56:38Z")

</div>

Thank you so much for the replies. I have tried to do after read what @brooksy125 told.

```python
while True:
    enemy = hero.findNearestEnemy()
    if enemy and hero.canCast("chain-lightning", enemy):
        hero.cast("chain-lightning", enemy)
    elif enemy and hero.canCast("drain-life", enemy):
        hero.cast("drain-life", enemy)
    else :
        hero.attack(enemy)

```

Worked pretty well now.  
Not forgetting to say thank you for the advice and kindness milton.jinich  
Also big thanks for the explanation and the advice @Lydia_Song I don’t know if I got right but I was supposed to replace the elif with another if ?

---

<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 10, 2020, 11:58pm UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/9 "2020-11-10T23:58:56Z")

</div>

hmm maybe change the last else to if and the elif to if and add an else ?

---

<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 11, 2020, 12:47am UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/10 "2020-11-11T00:47:10Z")

</div>

Oh I meant

```python
while True:
       enemy = hero.findNearestEnemy()
       if enemy:
            #check if you can cast your spell
                  #cast spell
            #check if you can cast your other spell
                  #cast spell
       else:
            #attack enemy

```

Lydia

---

<div class="post-metadata">

**Author:** ![ShiniG](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@ShiniG](https://discourse.codecombat.com/u/ShiniG)\
**Post date:** [November 11, 2020, 1:31am UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/11 "2020-11-11T01:31:12Z")

</div>

Thank you again for your explanation. I have nested the if statements and I could finish better this level.

```python
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.canCast("chain-lightning", enemy):
            hero.cast("chain-lightning", enemy)
        if hero.canCast("drain-life", enemy):
            hero.cast("drain-life", enemy)
        else:
            hero.attack(enemy)

```

---

<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 11, 2020, 1:32am UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/12 "2020-11-11T01:32:17Z")

</div>

Did you pass the level?  
Lydia

---

<div class="post-metadata">

**Author:** ![ShiniG](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@ShiniG](https://discourse.codecombat.com/u/ShiniG)\
**Post date:** [November 11, 2020, 1:33am UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/13 "2020-11-11T01:33:12Z")

</div>

Yes, with more health now.

---

<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 11, 2020, 1:38am UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/14 "2020-11-11T01:38:40Z")

</div>

Congrats! Please remove any solutions and be sure to check the tick mark next to someone’s comment that helped you the most!  
Lydia

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [November 11, 2020, 1:48am UTC](https://discourse.codecombat.com/t/solved-doom-glade-little-code-error/25309/15 "2020-11-11T01:48:29Z")

</div>

Nice adjustment @ShiniG!

One last note, you could have left it as “if, elif and else” for the different attacks. It helps prioritize which one you want to run by only running one of these codes depending on which ones are available at that time.

Right now it will always check for both chain-lightning and drain-life, sometimes that is what you want while other times you want to pick the first one that is available with the “if, elif and else”. There are a few that kind of conflict with each other since some attacks take some time to use while trying to call a more priority attack. Little details that will help later on.
