# \[SOLVED\] Keeping Time Help Needed

**URL:** <https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536>\
**Category:** Level Help\
**Created:** [April 24, 2018, 1:08am UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536 "2018-04-24T01:08:13Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![henryz111](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/henryz111/32/8009_2.png) [@henryz111](https://discourse.codecombat.com/u/henryz111)\
**Post date:** [April 24, 2018, 1:08am UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/1 "2018-04-24T01:08:13Z")

</div>

Using this code, my hero just stands there and not do anything. What is wrong with this?

```python
# Use your new skill to choose what to do: hero.time

while True:
    # 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:
            coin = hero.findNearestItem
            hero.moveXY(coin.pos.x, coin.pos.y)
        pass
    # After 35 seconds, attack again!
    else:
        hero.attack(enemy)
        pass

```

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [April 24, 2018, 1:29am UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/2 "2018-04-24T01:29:09Z")

</div>

add `()` to the end of the 6th line of code and in the 12th line of code.

---

<div class="post-metadata">

**Author:** ![henryz111](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/henryz111/32/8009_2.png) [@henryz111](https://discourse.codecombat.com/u/henryz111)\
**Post date:** [April 24, 2018, 1:30am UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/3 "2018-04-24T01:30:56Z")

</div>

I tried it, same results.

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [April 24, 2018, 1:36am UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/4 "2018-04-24T01:36:10Z")

</div>

1. You have a conditional statement, if enemy, before you define the variable, enemy.
2. You have a conditional statement, if coin, before you define the variable, coin.

You must define your variables first before using/calling them.

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [April 24, 2018, 1:36am UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/5 "2018-04-24T01:36:27Z")

</div>

I mean like `coin = hero.findNearestItem()`

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [April 24, 2018, 1:37am UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/6 "2018-04-24T01:37:24Z")

</div>

Also, check if the enemy exists after 35 seconds.

---

<div class="post-metadata">

**Author:** ![henryz111](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/henryz111/32/8009_2.png) [@henryz111](https://discourse.codecombat.com/u/henryz111)\
**Post date:** [April 24, 2018, 1:47am UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/7 "2018-04-24T01:47:04Z")

</div>

This worked. Thanks.

---

<div class="post-metadata">

**Author:** ![Rafi\_Kaufman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rafi_kaufman/32/13772_2.png) [@Rafi\_Kaufman](https://discourse.codecombat.com/u/Rafi_Kaufman)\
**Post date:** [November 6, 2020, 3:14pm UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/8 "2020-11-06T15:14:30Z")

</div>

this is the wrong code can you help me.

# Use your new skill to choose what to do: hero.time

while True:  
# 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:  
coin = hero.findNearestItem  
hero.moveXY(coin.pos.x, coin.pos.y)  
pass  
# After 35 seconds, attack again!  
else:  
hero.attack(enemy)  
pass

```python

```

---

<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 6, 2020, 3:53pm UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/9 "2020-11-06T15:53:27Z")

</div>

Could you please post your code correctly as it says in [this](https://discourse.codecombat.com/t/essentials-how-to-post-format-your-code-correctly/15521)  
Lydia

---

<div class="post-metadata">

**Author:** ![Rafi\_Kaufman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rafi_kaufman/32/13772_2.png) [@Rafi\_Kaufman](https://discourse.codecombat.com/u/Rafi_Kaufman)\
**Post date:** [November 12, 2020, 5:46pm UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/10 "2020-11-12T17:46:59Z")

</div>

can you please help me solve this code in javascipt.  
// Night is coming! Move all the soldiers towards the fire.  
function centerFormation(event) {  
// event.target is the unit running this event handler.  
var unit = event.target;  
// Now use unit.moveXY to move the unit to the fire.

}

// This spawns the four soldiers:  
game.spawnXY(“soldier”, 16, 57);  
game.spawnXY(“soldier”, 15, 13);  
game.spawnXY(“soldier”, 63, 13);  
game.spawnXY(“soldier”, 67, 57);

// This sets the soldier’s spawn action to the function centerFormation:  
game.setActionFor(“soldier”, “spawn”, centerFormation);

---

<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 12, 2020, 5:48pm UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/11 "2020-11-12T17:48:22Z")

</div>

Please post your code formatted correctly.  
Lydia

---

<div class="post-metadata">

**Author:** ![Jacob\_Thomas](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jacob_thomas/32/32737_2.png) [@Jacob\_Thomas](https://discourse.codecombat.com/u/Jacob_Thomas)\
**Post date:** [December 7, 2022, 12:36am UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/12 "2022-12-07T00:36:49Z")

</div>

```python
# Use your new skill to choose what to do: hero.time

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:
        items = hero.findItems()
        
        if items:
            hero.moveXY(items.pos.x, items.pos.y)
    # After 35 seconds, attack again!
    elif hero.time>35: 
        hero.attack(enemy)
    else:
        hero.moveXY(item.pos.x, item.pos.y)
        pass

```

I have no idea what I did wrong, please help.

---

<div class="post-metadata">

**Author:** ![MarmiteOnToast](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/marmiteontoast/32/46649_2.png) [@MarmiteOnToast](https://discourse.codecombat.com/u/MarmiteOnToast)\
**Post date:** [December 7, 2022, 6:46am UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/13 "2022-12-07T06:46:26Z")

</div>

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.  
Have a great time! 🥳

> [@Jacob\_Thomas](#):
>
> `hero.attack(enemy)`

Shouldn’t both of the `hero.attack(enemy)`s be in `if enemy:` loops?

---

<div class="post-metadata">

**Author:** ![Jacob\_Thomas](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jacob_thomas/32/32737_2.png) [@Jacob\_Thomas](https://discourse.codecombat.com/u/Jacob_Thomas)\
**Post date:** [December 7, 2022, 8:52pm UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/14 "2022-12-07T20:52:39Z")

</div>

Can you demonstrate what you mean by that, please?

---

<div class="post-metadata">

**Author:** ![MarmiteOnToast](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/marmiteontoast/32/46649_2.png) [@MarmiteOnToast](https://discourse.codecombat.com/u/MarmiteOnToast)\
**Post date:** [December 7, 2022, 8:54pm UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/15 "2022-12-07T20:54:31Z")

</div>

So, where you have

> [@Jacob\_Thomas](#):
>
> `hero.attack(enemy)`

in your code, surround them with an if enemy loop to make them look like this:

```python
if enemy:
    hero.attack(enemy)

```

---

<div class="post-metadata">

**Author:** ![Jacob\_Thomas](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jacob_thomas/32/32737_2.png) [@Jacob\_Thomas](https://discourse.codecombat.com/u/Jacob_Thomas)\
**Post date:** [December 7, 2022, 9:01pm UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/16 "2022-12-07T21:01:09Z")

</div>

```python
while True:
    # If it's the first 10 seconds, attack.
    if hero.time < 10:
        enemy= hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        pass
    # Else, if it's the first 35 seconds, collect coins.
    elif hero.time < 35:
        items = hero.findNearestItem()
        if items:
            hero.moveXY(items.pos.x, items.pos.y)
    # After 35 seconds, attack again!
    elif hero.time > 35: 
        if enemy:
            hero.attack(enemy)
    else:
        hero.moveXY(item.pos.x, item.pos.y)
        pass

```

like this?

---

<div class="post-metadata">

**Author:** ![MarmiteOnToast](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/marmiteontoast/32/46649_2.png) [@MarmiteOnToast](https://discourse.codecombat.com/u/MarmiteOnToast)\
**Post date:** [December 7, 2022, 9:17pm UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/17 "2022-12-07T21:17:38Z")

</div>

Yep

> [@Jacob\_Thomas](#):
>
> `enemy= hero.findNearestEnemy()`

Also move this to the top of the while true loop (just below where it says: `# If it's the first 10 seconds, attack.`)

Anyway, I’ve got to go now, sorry.

---

<div class="post-metadata">

**Author:** ![Jacob\_Thomas](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jacob_thomas/32/32737_2.png) [@Jacob\_Thomas](https://discourse.codecombat.com/u/Jacob_Thomas)\
**Post date:** [December 7, 2022, 10:09pm UTC](https://discourse.codecombat.com/t/solved-keeping-time-help-needed/14536/18 "2022-12-07T22:09:49Z")

</div>

Thank you! It worked after I changed a few more things.
