# Bookkeeper Help please

**URL:** <https://discourse.codecombat.com/t/bookkeeper-help-please/23384>\
**Category:** Level Help\
**Created:** [August 5, 2020, 7:18pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384 "2020-08-05T19:18:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sage\_Castillo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sage_castillo/32/13499_2.png) [@Sage\_Castillo](https://discourse.codecombat.com/u/Sage_Castillo)\
**Post date:** [August 5, 2020, 7:18pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/1 "2020-08-05T19:18:56Z")

</div>

````python
# This function allows to fight until the certain time
# and report about defeated enemies.
def fightAndReport(untilTime):
    defeated = 0
    while True:
        enemy = hero.findNearestEnemy()
       
        if enemy:
            hero.attack(enemy)
            enemy.health <= 0
            defeated += 1
        if hero.time > untilTime:
            break
    hero.moveXY(59, 33)
    hero.say(defeated)
    

# Fight 15 seconds and tell Naria how many enemies you defeated.
fightAndReport(15)

# Collect coins until the clock reaches 30 seconds.
def collectAndReport(untilTime):
    coin = hero.findNearestItem()
    collected = 0
    while True:
        coin = hero.findNearestItem()
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
            
        if hero.time > untilTime:
            break
    hero.moveXY(59, 33)
    hero.say(hero.gold)

collectAndReport(30)
# Tell Naria how much gold you collected.
hero.say(hero.gold)

# Fight enemies until the clock reaches 45 seconds.
fightAndReport(45)```
````

---

<div class="post-metadata">

**Author:** ![Sage\_Castillo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sage_castillo/32/13499_2.png) [@Sage\_Castillo](https://discourse.codecombat.com/u/Sage_Castillo)\
**Post date:** [August 5, 2020, 7:19pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/2 "2020-08-05T19:19:42Z")

</div>

for some reason when im done with the first round of enemies, my character always says 24 instead of 6 for enemies defeated. ive changed the value to defeated += .25 and that will give me 6 but then give me the wrong answer on the last round of defeating enemies. Im ready to pull my hair out because Im pretty sure this is the correct code.

on an unrelated note:

as much as I love code combat and im learning a lot, this program seems pretty buggy all the time, like I will get success when I run my code (in other levels) and then upon submission it says that I failed, if I submit 2 or 3 more times then the code gets accepted and I pass.

---

<div class="post-metadata">

**Author:** ![MidnightWolf39](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/midnightwolf39/32/13309_2.png) [@MidnightWolf39](https://discourse.codecombat.com/u/MidnightWolf39)\
**Post date:** [August 5, 2020, 7:26pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/3 "2020-08-05T19:26:02Z")

</div>

> [@Sage\_Castillo](#):
>
> `enemy.health <= 0`

should be

```python
if enemy.health <= 0:

```

---

<div class="post-metadata">

**Author:** ![Sage\_Castillo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sage_castillo/32/13499_2.png) [@Sage\_Castillo](https://discourse.codecombat.com/u/Sage_Castillo)\
**Post date:** [August 5, 2020, 7:31pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/4 "2020-08-05T19:31:12Z")

</div>

![Screen Shot 2020-08-05 at 3.28.26 PM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/0/0e7bd94e2afe2b7e81bbd937a3825781ce145749.jpeg)

thank you for the pointer but I think there is still something else missing.

I got this error when I tried that.

---

<div class="post-metadata">

**Author:** ![Gamespoiler](https://avatars.discourse-cdn.com/v4/letter/g/77aa72/32.png) [@Gamespoiler](https://discourse.codecombat.com/u/Gamespoiler)\
**Post date:** [August 5, 2020, 7:47pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/5 "2020-08-05T19:47:11Z")

</div>

You need to indent that more

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [August 5, 2020, 7:53pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/6 "2020-08-05T19:53:57Z")

</div>

Actually, the indent is fine…what happens if there is no enemy?

You’ve stacked multiple ‘if’ statements, which means they are each independent of the others. Instead, you should combine them in to fewer statements, or else nest them.

---

<div class="post-metadata">

**Author:** ![MidnightWolf39](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/midnightwolf39/32/13309_2.png) [@MidnightWolf39](https://discourse.codecombat.com/u/MidnightWolf39)\
**Post date:** [August 5, 2020, 7:56pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/7 "2020-08-05T19:56:47Z")

</div>

I think what @Gamespoiler means is that @Sage_Castillo needs to indent

```python
if enemy.health <= 0

```

more so that the hero only checks the enemy’s health if there is an enemy.

---

<div class="post-metadata">

**Author:** ![Gamespoiler](https://avatars.discourse-cdn.com/v4/letter/g/77aa72/32.png) [@Gamespoiler](https://discourse.codecombat.com/u/Gamespoiler)\
**Post date:** [August 5, 2020, 7:57pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/8 "2020-08-05T19:57:22Z")

</div>

yes that is what I meant

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [August 5, 2020, 8:00pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/9 "2020-08-05T20:00:07Z")

</div>

Aha…that would be nesting them then. I didn’t realize that is what you meant.

---

<div class="post-metadata">

**Author:** ![Sage\_Castillo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sage_castillo/32/13499_2.png) [@Sage\_Castillo](https://discourse.codecombat.com/u/Sage_Castillo)\
**Post date:** [August 5, 2020, 8:02pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/10 "2020-08-05T20:02:19Z")

</div>

````python
def fightAndReport(untilTime):
    defeated = 0
    while True:
        enemy = hero.findNearestEnemy()
       
        if enemy:
            hero.attack(enemy)
            if enemy.health <= 0:
                defeated += 1
        if hero.time > untilTime:
            break
    hero.moveXY(59, 33)
    hero.say(defeated)
    

# Fight 15 seconds and tell Naria how many enemies you defeated.
fightAndReport(15)

# Collect coins until the clock reaches 30 seconds.
def collectAndReport(untilTime):
    coin = hero.findNearestItem()
    collected = 0
    while True:
        coin = hero.findNearestItem()
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
            
        if hero.time > untilTime:
            break
    hero.moveXY(59, 33)
    hero.say(hero.gold)

collectAndReport(30)
# Tell Naria how much gold you collected.
hero.say(hero.gold)

# Fight enemies until the clock reaches 45 seconds.
fightAndReport(45)```
````

---

<div class="post-metadata">

**Author:** ![Sage\_Castillo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sage_castillo/32/13499_2.png) [@Sage\_Castillo](https://discourse.codecombat.com/u/Sage_Castillo)\
**Post date:** [August 5, 2020, 8:07pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/11 "2020-08-05T20:07:28Z")

</div>

I used it like this and I’m still getting an error. The issue is that my character is saying 24 when i’ve only slain 6 enemy.

this same error was happening in double Cheek where I had to change the value associated with  
defeated += 1 to defeated += .5 in order to get my character to kill all the ogres and then go look for coins. the coins wouldn’t even generate until all the ogres were killed so it’d just be stuck.

once I changed the value to .5 the level worked like it should. THAT same thing is happening to me on book keeper. if I change the value to .25 then my character will say the correct number of defeated enemy. The other problem is that if I keep this .25 value for defeated += .25 then I won’t get the correct number for the 2nd round of defeating enemies. defeated += .25 yields me 4.75 instead of 5.

this leads me to believe the level is buggy. the whole function on the top part was already written by Codecombat so I don’t see how there could be a problem with their code.

---

<div class="post-metadata">

**Author:** ![Gamespoiler](https://avatars.discourse-cdn.com/v4/letter/g/77aa72/32.png) [@Gamespoiler](https://discourse.codecombat.com/u/Gamespoiler)\
**Post date:** [August 5, 2020, 8:11pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/12 "2020-08-05T20:11:55Z")

</div>

When I run your code, it works just fine for me. @dedreous @Deadpool198 can you guys figure out what is wrong?

---

<div class="post-metadata">

**Author:** ![Sage\_Castillo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sage_castillo/32/13499_2.png) [@Sage\_Castillo](https://discourse.codecombat.com/u/Sage_Castillo)\
**Post date:** [August 5, 2020, 8:27pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/13 "2020-08-05T20:27:54Z")

</div>

these kinds of bugs make learning very frustrating. Half the time I feel like im really making progress and the other Half I feel like an idiot because I can’t figure it out, only to realize that it was a bug anyways.

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [August 5, 2020, 8:35pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/14 "2020-08-05T20:35:13Z")

</div>

I would hesitate calling it a bug, since it works for others. If it truly was a bug, you would expect it to fail all the time. I am also able to run your code with no issues. Have you tried using a different browser?

> [@Sage\_Castillo](#):
>
> upon submission it says that I failed, if I submit 2 or 3 more times…

Means that the code you’ve written more closely fits a specific seed…each time you submit, you get a new seed, until you finally land on one that is a better match.

---

<div class="post-metadata">

**Author:** ![Sage\_Castillo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sage_castillo/32/13499_2.png) [@Sage\_Castillo](https://discourse.codecombat.com/u/Sage_Castillo)\
**Post date:** [August 5, 2020, 8:44pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/15 "2020-08-05T20:44:30Z")

</div>

I think you’ve got things backwards. if it failed everytime then you could say its bad code but the fact that it works for other people and not me says something. Like obviously my code must be good if other people can pass without editing it.

I appreciate everybody replying so fast, im just not any closer to understanding why this isn’t working.

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [August 5, 2020, 8:47pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/16 "2020-08-05T20:47:00Z")

</div>

So then, the task is to figure out what’s different between you and them…such as your browser, perhaps?

---

<div class="post-metadata">

**Author:** ![Sage\_Castillo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sage_castillo/32/13499_2.png) [@Sage\_Castillo](https://discourse.codecombat.com/u/Sage_Castillo)\
**Post date:** [August 5, 2020, 8:49pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/17 "2020-08-05T20:49:40Z")

</div>

why would using another browser make a difference? it just doesn’t make sense, I also don’t want to have to install another browser just to get through this level. Im using safari if that makes a difference.

I just want to understand your reasoning other than its just regular ol’ troubleshooting.

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [August 6, 2020, 8:06am UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/18 "2020-08-06T08:06:42Z")

</div>

Hi, again, as with the others, it works for me. So I think we should try and work out all the differences between your level environment and mine.  
Please could I have: a screenshot of your equipment and of the level screen when you say your amount of gold. Then some more info about any previous problems etc. Also please could I have a screenshot of your code in the level if you can fit it all in, or in two if not. (That may seem weird but it takes out any possibility of errors in transmission between you and me).  
Sorry about having to do all this, but you need a lot of info to find a bug.  
Danny  
P.S. I think the browser matters a bit more with loading related issues, but if nothing else works you might want to give it a try-- you never know!

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [August 6, 2020, 9:38pm UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/19 "2020-08-06T21:38:09Z")

</div>

This is funny 🙂  
The code is OK, but not for your hero and especially for lightning twig staff.  
See carefully the stats:  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/0/0b5c692f762b7fb5bb8fff0f87798bec1f29dcd9.png)  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/4/4c6044e8317237618c0a525c0e0d3896603e7232.png)  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/7/77be7b79d123336edd4202a56dab9e3c74ad53a8.png)

To kill a scout you need 50/13.36 = 3.74 attacks. You cannot interrupt an attack so you’ll need 4 attacks.  
The piece of code will be:

```python
    defeated = 0
    while True:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
            hero.attack(enemy)
            hero.attack(enemy)
            hero.attack(enemy)
            defeated += 1;
        if hero.time > untilTime:
            break
    hero.moveXY(59, 33)    
    hero.say(defeated)

```

You can make it also in a different way, but the reasoning it’s a little bit more obscure:

```python
        if enemy:
            startTime = hero.time
            while hero.time - startTime < 1:
                hero.attack(enemy)
            defeated += 1;

```

you’ll see many “… but it’s dead!” mesages

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/2/217731dff36f0a92109bf5cc90457b465c212d56.png)  
that I’m hating so much!  
The easiest way is to stick to a simple free hero 🙂

P.S.  
I think the best way to do the above trick is to initialize the weapon’s damage.

```python
twigDamage = 13.36

        enemy = hero.findNearestEnemy()
        if enemy:
            n = 0
            while enemy.maxHealth - n*twigDamage > 0:
                hero.attack(enemy)
                n +=1
            defeated += 1;  

```

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [August 7, 2020, 8:20am UTC](https://discourse.codecombat.com/t/bookkeeper-help-please/23384/20 "2020-08-07T08:20:23Z")

</div>

Thank you for that lovely solution @xython 😁. I especially like the n\*twigDamage, very clever.
