[Solved] Keeping Time

Sword of the temple guard’s attack cooldown is 0.26 seconds, but Morning’s edge has a cooldown of 0.25, so the morning’s edge is slightly faster.

@Chaboi_3000 I said the hero can only attack 4 times per second. That’s even more correct with the morning edge.

This is not working. It says that there is a function instead of a unit for the attacking at first

We can’t really help unless you post your code. Don’t forget to use the </> button so it formats correctly.

I fixed it because all there was was an extra enemy = hero.findnearestEnemy.

an extra enemy=hero.findNearestEnemy() shouldn’t cause a problem

It did because I put it in an if statement when I had one right outside of the statement.

1 Like

This code isnt workingwhile True: # If it's the first 10 seconds, attack. if hero.time < 10: enemy = hero.findNearestEnemy() hero.attack(enemy) pass # Else, if it's the first 35 seconds, collect coins. elif hero.time < 35: coin = hero.findNearestItem() if coin: hero.moveXY(coin.pos.x, coin.pos.y) pass # After 35 seconds, attack again! else: hero.attack(enemy)

1 Like

Could you start a new line then paste your code?

1 Like
# Get to the oasis,
# fencing off paths with yaks on them as you go.
while True:
    yak = self.findNearestEnemy()
    if yak:
        # A yak is above you if its y is greater than your y.
        # If the yak is above you, build a fence 10m below it.
        if yak.pos.y > self.pos.y :
            self.buildXY("fence", yak.pos.x, yak.pos.y -10)
        # If the yak is below you, build a fence 10m above it.
        else :
            if yak.pos.y < self.pos.y :
                self.buildXY("fence", yak.pos.x, yak.pos.y +10)
    else:
        # Move right 10m towards the oasis.
        self.moveXY(self.pos.x +10, self.pos.y)

i need so much help please

Are you using code written by someone else… :wink: Cheeky.
Unless for some reason you’ve chosen to write it using self rather than hero, you must have copied it from somewhere else.
If you write some code for the level, after making sure you’ve read the hints and all the other topics on the forum about this level then I’ll look at it and help you.
Thanks
Danny

4 Likes

i really need some help with the Keeping Time level. will this code work

# If it's the first 10 seconds, attack.
if hero.time < 10:
    if enemy:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)
    pass
# Else, if it's the first 35 seconds, collect coins.
elif hero.time < 35:
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)
    pass
# After 35 seconds, attack again!
elif hero.time > 35:
    if enemy:
        hero.attack(enemy)
    if hero.isReady("cleave"):
        hero.cleave(enemy)
    if hero.health > 40:
        hero.moveXY(16, 38)
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)
    else:
        hero.attack(enemy)
        hero.shield()
        hero.bash(enemy)
2 Likes

Hi TENTEN_CODER,

Welcome to the forum!

In answer to your question ‘will this work’, I’d say try it and see - it’s the best way to get feedback, and you’ll learn about what different error codes look like.

However, a couple of things. What’s the problem with these two lines?

    if enemy:
        enemy = hero.findNearestEnemy()

And think about where you define the variable coin as well!

Hope that helps; if you’re still having trouble then post your new code again :slight_smile:.

Jenny

2 Likes

while True:
# If it’s the first 10 seconds, attack.
if hero.time < 10:
enemy = hero.findNearestEnemy()
hero.attack(enemy)
pass
# Else, if it’s the first 35 seconds, collect coins.
elif hero.time < 35:
item = hero.findNearestItem()
if item:

        hero.moveXY(item.pos.x, item.pos.y)
    pass
# After 35 seconds, attack again!
else:
    if enemy:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)

hero keeps taking coins after 35 second and not attacking enemies pls help me fix my code

1 Like

Welcome to the forum! This is a family-friendly place where coders can share bugs, ask for help on any CodeCombat level (don’t forget to post your code correctly), or just hang out with other coders. But before you proceed, please check out our guidelines: this topic.
Have a great time! :partying_face:

Can you please format your entire code? Then I will try to to help :slightly_smiling_face:.

2 Likes

thank you im already solving it :grinning:

2 Likes

my code says “indentation error” so i have no idea

here is my code

while True:
    # If it's the first 10 seconds, attack.
    enemy = hero.findNearestEnemy()
    if hero.time < 10:
        hero.attack(enemy)
        pass
    # Else, if it's the first 35 seconds, collect coins.
    coin = hero.findNearestItem()
    elif hero.time < 35:
        hero.move(coin.Pos)
    # After 35 seconds, attack again!
    else:
        hero.attack(enemy)
        pass

for some reason it says “indentation error” on the elif and else so please help me

you cant put code in between the elif and if, try making the elif this way:
if hero.time > 10 and hero.time < 35: