# Help bookkeeper python

Hey there

i’m having trouble with the level Bookkeeper
CodeCombat - Coding games to learn Python and JavaScript?

Here is my code

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

collectGold(30)

# Tell Naria how much gold you collected.

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

So the hero doesnt move after having report the defeated …

What part? (forest, mountain etc…)

I don’t understand your question, what do you mean ?
I’m stuck in the level bookkeeper in the desert, is that what you mean ?

sorry I posted the same link, I don’t understand why I cant post the link that goes straight to the level, anyway it’s bookkepper in the desert

Thank you, that is what I mean. Just a sec.

In this, you are appending coin.value, that is wrong, look up above to you first def to get some help.

I’ve changed by this

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

Now the hero goes to collect the coins but the goal " Tell Naria how much gold you collected." isnt’t accomplished…

this part will just have the hero say what the coin is
try replacing it with
hero.say(hero.gold)

So I’ve tried this

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

Still doesn’t work
error message says: “typeError: can’t write to protected property: gold”

this is giving you the error, remove it

I’ve tried but still the hero doesnt move

When I use this code:

def collectCoin(untilTime):
while True:
item = hero.findNearestItem()
coin = 0
if item:
hero.moveXY(item.pos.x, item.pos.y)
coin += 1
if hero.time > untilTime:
break
hero.moveXY(59, 33)
hero.say(coin)

The hero goes to collect the coin, on the top right, the number of coins collected appear, but when the hero goes to tell how much gold he collected, he says 1 instead of the number that appears on the top left …

So hero.gold is a set variable that will always return the amount of gold you have.
If you have 40 gold for example, doing this: hero.say(hero.gold) will get the hero saying 40, so no need to define it. (using coin for example)
What you are required to do in this function is to find an item, if it exists, move to its position, if the time is greater than untilTime, break.
Hint:

Remove both lines where you mention coin, and put hero.gold instead of coin in the say part

1 Like

OMG thanks a lot

I’m really new in coding so I still don’t understand everything, specially the vocabulary like variable etc …

thanks!

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.