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
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 :)
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)
@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.
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
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.
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).