Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[Solved] Operation "Killdeer" Help


#1

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!


#2

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.


#3

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. ;(


#5

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)


#6

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.


#7

Does this look correct?


#8
    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)


#9

Is somebody there? Anyone?


#10

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.


#11

How do I do that and what does that mean


#12

i have to leave, but i will be back tomorrow


#13

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

if something() is True:
    do this

#14

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


#15
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?

#16

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


#17

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!