[Solved] Operation "Killdeer" Help

This is my code:

def shouldRun():
    if hero.health < hero.maxHealth / 2:
        return True
    else:
        return False

while True:
    # Move to the X only if shouldRun() returns True
    if shouldRun():
        hero.moveXY(75, 37)
    # Else, attack!
    else:
        hero.attack(enemy)

But when I run my code, the hero kills one ogres before letting the other ogres kill him. Please help!

Inside your while True loop, what is an enemy? You need to define the variable. Also, it’s a good idea to get in the habit of checking for the existence of an enemy before attacking it. You’re going to run into that a lot so save yourself the headache and just start doing it now. In this situation, you can use elif enemy instead of else.

1 Like

Usually a duck hero will tell you to do that, as an error message. Probably to motivate kids,I don’t think it motivates me at all. ;(

I can’t figure this out! My hero does nothing. Please help!
my code is this:def shouldRun():
if hero.health < hero.maxHealth / 2:
return True
else:
return False

while True:
# Move to the X only if shouldRun() returns True
if def shouldRun():
hero.moveXY(75, 37)
# Else, attack!
else:
enemy = hero.findNearestEnemy()
enemy = hero.attack(enemy)

Please learn to post your code correctly. The way it is now, we can’t see the structure. Help us help you. It’s very easy to do and just takes a tiny bit of effort.

Does this look correct?

    if hero.health < hero.maxHealth / 2:
        return True
    else:
        return False

while True:
    # Move to the X only if shouldRun() returns True
    hero.moveXY(75, 37)
    # Else, attack!
    enemy = hero.findNearestEnemy()
    hero.attack(enemy)
    hero.powerUp()
    hero.bash(enemy)
    hero.attack(enemy)

Is somebody there? Anyone?

Be sure that ALL of your code is formatted properly from now on, not just most of it. Structure is essential to programming and we can only assume that the code for your function is still formatted properly since it’s part of the supplied/initial code. That being said, you have a function, but then never call or use that function. The initial comments tell you what to do.

# Move to the X only if shouldRun() returns True

You haven’t done that. Also, in your while True loop, you never check to see if an enemy exists before attacking. You should always do this.

How do I do that and what does that mean

i have to leave, but i will be back tomorrow

1 Like

The moveXY code is already there. Under what conditions should you do that?

if something() is True:
    do this

Also you don’t need to check if something is true like this, if Nan=True: instead you can just do if Nan:

type or paste code here
```# Lure the ogres into a trap. These ogres are careful.
# They will only follow if the hero is injured.

# This function checks the hero's health 
# and returns a Boolean value.
def shouldRun():
    if hero.health < hero.maxHealth / 2:
        return True
    else:
        return False

while True:
    # Move to the X only if shouldRun() returns True
    hero.moveXY(75, 37)
    # Else, attack!
    enemy = hero.findNearestEnemy()
    hero.attack(enemy)
    hero.powerUp()
    hero.bash(enemy)
    hero.attack(enemy)

What am I doing wrong?

Never mind! I fixed something I thought needed fixing and it worked!

I have the same problem but when I put an else if or elif statement, it has an error, so what do I do then to fix it? Edit: Nevermind, I found my error!

2 Likes

while True:
# Move to the X only if shouldRun() returns True
if shouldRun() return true:
hero.moveXY(75, 37)
# Else, attack!
the if shouldRun statement is saying that it needs another colon at the end but I did that then it says it still needs another one. please help!!!

Hi! I don’t understand what’s wrong with my code. It only lets me kill 5(including the one at the end which the villagers kill). I never kill the large ogre. Please help.

def shouldRun():
    if hero.health < hero.maxHealth / 2:
        return True
    else:
        return False

while True:
    # Move to the X only if shouldRun() returns True
    hero.moveXY(75, 37)
    # Else, attack!
    enemy = hero.findNearestEnemy()
    hero.attack(enemy)

Before that check and only attack the enemy if there is one.

Andrei

Thanks! I actually figured it out… I was supposed to add an “else” and put the “enemy = hero.findNearestEnemy()” before the else.

1 Like