[Solved] The One Wizard won't work

I came back to this level after awhile, and I don’t understand why my code’s not working. Eric Tang already tried to help, but he was stumped to. Here’s my code:

# Defeat as many ogres as you can.
# Use 'cast' and 'canCast' for spells.

while True:
    enemy = hero.findNearestEnemy()
    
    if enemy and hero.distanceTo(enemy) <= 10 and enemy.health > 0:
        if hero.canCast("drain-life", enemy):
            hero.cast("drain-life", enemy)
    elif enemy and hero.distanceTo(enemy) <= 10 and enemy.health > 0:
        if hero.canCast("chain-lightning", enemy):
            hero.cast("chain-lightning", enemy)
    if enemy.type == "ogre":
        hero.moveXY(7, 35)

It’s this line that’s always being caught:

if enemy.type == "ogre":

If you’re wondering why I’m finding an ogre type, it’s because of the hint here:

image

You didn’t check if there’s even an enemy existing before checking if the enemy type is “ogre”. Always check if there’s an enemy before doing anything with it.

Well, it’s not stopping me anymore, but my code still won’t work.

# Defeat as many ogres as you can.
# Use 'cast' and 'canCast' for spells.

while True:
    enemy = hero.findNearestEnemy()
    
    if enemy and hero.distanceTo(enemy) <= 10 and enemy.health > 0:
        if hero.canCast("drain-life", enemy):
            hero.cast("drain-life", enemy)
    elif enemy and hero.distanceTo(enemy) <= 10 and enemy.health > 0:
        if hero.canCast("chain-lightning", enemy):
            hero.cast("chain-lightning", enemy)
    if enemy:
        if enemy.type == "ogre":
            hero.moveXY(7, 35)


Where did the “drain-life” part come from? The hero doesn’t have that ability in the level.

Well, it was in the hints more than once. So I’d thought that it ought to be able to work. I will change that.

Do you mean “regen”? I am looking at hints right now and there is nothing about “drain-life”.

Oh, it was here:

and I changed the spell, but it still won’t work.

# Defeat as many ogres as you can.
# Use 'cast' and 'canCast' for spells.

while True:
    enemy = hero.findNearestEnemy()
    
    if enemy and hero.distanceTo(enemy) <= 10 and enemy.health > 0:
        if hero.canCast("lightning-bolt", enemy):
            hero.cast("lightning-bolt", enemy)
    elif enemy and hero.distanceTo(enemy) <= 10 and enemy.health > 0:
        if hero.canCast("chain-lightning", enemy):
            hero.cast("chain-lightning", enemy)
    if enemy:
        if enemy.type == "ogre":
            hero.moveXY(7, 35)

The spell was there, and when I start typing the

hero.canCast("whatever"):

and it tells me to push enter for a shortcut so it writes it instead of me, they write “drain-life” as the starter.

Yikes. While I was working on the mass-grow ability on chain-lightning component, it seems like it didn’t revert changes on chain-lightning and saved, so it maintains “healing”. I just pushed a fix. Can you try doing the level on direct.codecombat.com?

Nope. It still doesn’t work.

# Defeat as many ogres as you can.
# Use 'cast' and 'canCast' for spells.

while True:
    enemy = hero.findNearestEnemy()
    
    if enemy and hero.distanceTo(enemy) <= 10 and enemy.health > 0:
        if hero.canCast("lightning-bolt", enemy):
            hero.cast("lightning-bolt", enemy)
    elif enemy and hero.distanceTo(enemy) <= 10 and enemy.health > 0:
        if hero.canCast("chain-lightning", enemy):
            hero.cast("chain-lightning", enemy)
    if enemy:
        if enemy.type == "ogre":
            hero.moveXY(7, 35)

First, you should never “sandwich” an elif statement between two if statements. It’ll get confusing once you start writing longer codes. Second, you are always checking for an enemy for all 3 statements. Can you try putting them into one since “if enemy” statement and removing enemy checks for each one?

Next, you are checking for the same condition for both spells. Combine them within one elif statement like:

elif hero.distanceTo(enemy) <= 10 and enemy.health > 0:
            if hero.canCast("lightning-bolt",enemy):
                hero.cast("lightning-bolt", enemy)
            elif hero.canCast("chain-lightning", enemy):
                hero.cast("chain-lightning", enemy)

Since running away from an ogre is the most vital part of code, you should put it as an if-statement at the top. Have the elif statement after the if-statement(For casting spells).

1 Like

Thank you so much! It worked.

Should I delete any of my codes on here do you think?

It’s not a complete solution, so you can leave it be :slight_smile:

1 Like

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