[SOLVED] Bookkeeper help!

Hi! I’m having trouble with the Bookkeeper level.
I am getting the following error on line 10:
Fix Your Code
Try self.health
Line 10: Cannot read property ‘health’ of null.

I can think of two possible things right now:
1- I am not defining the enemy variable properly or
2 - I need different equipment (sense stone) to sense enemy health levels and I don’t have it equipped.

Any help/ ideas would be much appreciated! Thanks in advance :slight_smile:

Here is a screenshot:

My code is below:

Fight enemies for 15 seconds.

# Keep count whenever an enemy is defeated.

while True:
    defeated = 0
    enemy = self.findNearestEnemy()
    if self.now >=15:
        break
    else:
        if enemy.health <=0:
            defeated += 1
        else:
            self.attack(enemy)
            self.attack(enemy)
# Tell Naria how many enemies you defeated.
self.moveXY(59, 33)
self.say("I've defeated "+defeated+" enemies")
# Collect coins until the clock reaches 30 seconds.
while True:
    if self.now >=30:
        break
    else:
       coin = self.findNearestItem()
       cPos = coin.pos
       cx = cPos.x
       cy = cPos.y
       self.move(cx,cy)
# Tell Naria how much gold you collected.
self.moveXY(59, 33)
self.say("I've collected "+coin+" coins")
# Fight enemies until the clock reaches 45 seconds.
while True:
    if self.now >=15:
        break
    else:
# Remember to reset the count of defeated enemies!
        defeated = 0
        self.attack(enemy)
        if enemy.health <= 0:
            defeated += 1
# Tell Naria how many enemies you defeated.
self.moveXY(59, 33)
self.say("I've defeated "+defeated+" enemies")

this doesn’t work because there could be enemies that were already counted in the index, but you counted them again.

Hi.I also need help with this level. When i first run this code it worked fine, but when i submited it 2/3 phases were fine but on second wave i got wrong number of defeated ogers. When i run it again i got number of defeted ogres in first wave x2.

My code look like this.

def goNaria():
    hero.moveXY(59, 33)
# Fight enemies for 15 seconds.
killed=0
# Keep count whenever an enemy is defeated.
while True:
    enemy=hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave") and hero.distanceTo(enemy)<10:
            hero.cleave(enemy)
            if enemy.health<=0:
                killed+=1
        elif hero.isReady("bash"):
            hero.bash(enemy)
            if enemy.health<=0:
                killed+=1
        else:
            hero.attack(enemy)
            hero.shield()
            if enemy.health<=0:
                killed+=1

    if hero.now()>15:
        break
# Tell Naria how many enemies you defeated.
goNaria()
hero.say(killed)
# Collect coins until the clock reaches 30 seconds.
while True:
    coin=hero.findNearestItem()
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)
    if hero.now()>30:
        break
# Tell Naria how much gold you collected.
goNaria()
hero.say(hero.gold)
# Fight enemies until the clock reaches 45 seconds.
# Remember to reset the count of defeated enemies!
killed=0
while True:
    enemy=hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave") and hero.distanceTo(enemy)<10:
            hero.cleave(enemy)
            if enemy.healt<=0:
                killed+=1
        elif hero.isReady("bash"):
            hero.bash(enemy)
            if enemy.health<=0:
                killed+=1
        else:
            hero.attack(enemy)
            hero.shield()
            if enemy.health<=0:
                killed+=1
    if hero.now()>45:
        break
# Tell Naria how many enemies you defeated.
goNaria()
hero.say(killed)
killed=0 #this i added just in case :)

Can you please format your code by putting triple backticks( ` ) around it? We need to see the indentations in Python.

Some descriptive text

Fight enemies for 15 seconds.
Keep count whenever an enemy is defeated.
I edited it hope it can help you guys. you have to make sure you understand previous steps about if/ else and break.

attainment = 0
while True:
    enemy = self.findNearestEnemy()
    if enemy:
        self.attack(enemy)
        if enemy.health <=0:
            attainment += 1
    else:
        self.moveXY(58, 33)    
    if self.now()>=15:
        break;
# Tell Naria how many enemies you defeated.

self.moveXY(58, 33)
self.say(attainment)
attainment=0
# Collect coins until the clock reaches 30 seconds.
while True:
    item = self.findNearestItem()    
    if item:
        self.moveXY(item.pos.x, item.pos.y)        
    else:
        self.moveXY(58, 33)    
    if self.now()>=30:
        break;
# Tell Naria how much gold you collected.
```
self.moveXY(58, 33)
self.say(self.gold)
```
# Fight enemies until the clock reaches 45 seconds.
# Remember to reset the count of defeated enemies!
attainment = 0;
while True:
    enemy = self.findNearestEnemy()
    if enemy:
        self.attack(enemy)
        if enemy.health <=0:
            attainment += 1
    else:
        self.moveXY(58, 33)    
    if self.now()>=45:
        break;
# Tell Naria how many enemies you defeated.
```
self.moveXY(58, 33)
self.say(attainment)

Do it according to the discourse FAQ please!

@Jiayao_Wang, please give more information about what is happening if anything is. The reason why I mentione theat is because if you were trying to help anyone by giving out the code, please don’t.

It makes it easier for users to pass the level, but it doesn’t teach them coding at all because all their doing is rushing through it.

What you can do though is give them little hints about what they did wrong.

Cheers! :beers:

Please :heart: this post if it helps

2 Likes

yo i need help with this.
I need python code though this is what i have so far
Fight enemies for 15 seconds.
Keep count whenever an enemy is defeated.
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > 15:
break

Tell Naria how many enemies you defeated.

hero.moveXY(59, 33)
hero.say(defeated)

Collect coins until the clock reaches 30 seconds.

Tell Naria how much gold you collected.

hero.say(hero.gold)

Fight enemies until the clock reaches 45 seconds.

Remember to reset the count of defeated enemies!

Tell Naria how many enemies you defeated.

That part of the code is telling the hero that if the enemy is dead attack.
It should be if enemy.health >= 0:.

1 Like

So this is what I got (this is in python)

defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > 15:
break

hero.moveXY(59, 33)
hero.say(defeated)

hero.gold = 0
coin = hero.findNearestItem()
while True:
if “coin”:
hero.moveXY(coin.pos.x, coin.pos.y)
hero.gold += 1
if hero.time > 30:
break

hero.say(hero.gold)

defeated = 0
enemy = hero.findNearestEnemy()
while True:
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > 45:
break
The problem is that at the hero.gold = 0 part the site says that it can’t write to protected property: gold
and i am going like what does that mean?

You are trying to create a variable with a protected property. Try using just gold (or whatever variable) instead of a protected property like hero.gold.

Also another problem that has nothing to do with your code’s protected property issue: You do not need to put quotation marks around coin.

In the future, remember to format your code using the triple backticks (one backtick looks like this: `) so it is easier to read. :slightly_smiling_face:

1 Like

i can’t use another variable. I tried doing gold but didn’t work

1 Like

Define coin inside the while-true loop. Do the same thing for the second attacking part.

1 Like

thanks
you the best
hellenar

2 Likes

I’m not the best, but I try. :wink:

1 Like

Hi. I need some help. This code isn’t working, yet I do not understand why. My character counts the defeated well, but not the coins. My character cannot seem to stop collecting coins and go to the x mark. Please help me debug it.

# 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)
            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 collectCoins(untilTime):
    totalGold = 0
    while True:
        item = hero.findNearestItem()
        hero.moveXY(item.pos.x, item.pos.y)
        totalGold += item.value
    if hero.time > untilTime:
        break
    hero.moveXY(59, 33)
    hero.say(totalGold)


# Tell Naria how much gold you collected.
collectCoins(30)

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

You are probably receiving a null error because in your collectCoins(untilTime) function you don’t check to see if item exists before moving to item. Also, at the end of this function, try hero.say(hero.gold) instead of hero.say(totalGold).

Thankyou very much for your help! I finally solved it!