# Bookkeeper code help

#1

ok i trying to complete this level and i’ve tried about a million times and i still cant figure it out
here’s what i got

``````while true:
enemies = self.findEnemies()
nearest_enemy = self.findNearest(enemies)
if enemies and nearest:
self.attack(nearest_enemy)
if nearest and nearest.health <=0:
defeated=defeated+1
if self.now() > 15:
self.moveXY(59, 33)
self.say( defeated )
break
while True:
items = self.findItems()
nearest_item = self.findNearest(items)
value.gold
if nearest_item:
self.move(nearest_item)
gold=gold+ value.gold
if self.now()>30:
self.moveXY(58, 33)
self.say(gold)
break
loop:
if self.now()=31:
defeated=0
break

while True:
enemies = self.findEnemies()
nearest_enemy = self.findNearest(enemies)
if enemies and nearest:
self.attack(nearest_enemy)
if nearest and nearest.health <=0:
defeated=defeated+1
if self.now() > 45:
self.moveXY(59, 33)
self.say( defeated )
break``````
#2

You should revise your use of `nearest` (Is it defined?), `value.gold` and maybe also `gold`.

You can use `nearest_item.value` to get its value in gold, and `self.gold` to get the number of gold your hero has. You could even ignore the gold values and just report `self.gold` at the end.

The block inside the middle `loop` can be combined with the code above.

#3

thanks. i still have a problem with telling naria the amount of defeats i have
if fixed what you said

``````while True:
enemies = self.findEnemies()
nearest_enemy = self.findNearest(enemies)
if enemies and nearest_enemy:
self.attack(nearest_enemy)
if nearest_enemy and nearest_enemy.health <=0:
defeated=defeated+1
if self.now() > 15:
self.moveXY(59, 33)
self.say( defeated )
break
while True:
items = self.findItems()
nearest_item = self.findNearest(items)
if nearest_item:
self.move(nearest_item)
if self.now()>30:
self.moveXY(58, 33)
self.say(self.gold)
defeated=0
break

while True:
enemies = self.findEnemies()
nearest_enemy = self.findNearest(enemies)
if enemies and nearest:
self.attack(nearest_enemy)
if nearest and nearest.health <=0:
defeated=defeated+1
if self.now() > 45:
self.moveXY(59, 33)
self.say( defeated )
break``````
#4

Hi! I tried your code and had same trouble (not surprising, heh? )
Try to define “defeated” equal to 0 before the first while loop.
Also, system said me that my/your code have some troubles with “nearest_enemy” and “nearest” in the third while loop. Be more careful with this. They differ, but should be the same)
And you forgot something in the part:

``````if nearest_item:
self.move(nearest_item)
``````

Anyway, you are close, with the couple of improvements I made your code work. Good luck!

#5

thanx i completed the level

#6

i even have a problem too. when i try:
if defeatedx == 7:
self.say(“i defeated 7 enemies”)
it won’t work! if i do other variables, i wont do it!

#7

Can anyone write a python code?

#8

Anyone can write one)) Joke)
It’s forbidden to post right solution, but you can post your code properly formatted and maybe someone could give you a hint or advice. Or you can take alex_1’s code and replies to hi post and complete it till the success.

1 Like
#9

i don’t get it what do you mean?

#10

Basically, you can’t post complete solutions because others who just want to pass the level will copy the solution without learning anything. However, you can post your code and others can give you snippets of advice about how to pass.
To properly format code as @Alexbrand stated, you can surround your code with triple backticks, which look like this: `
This:
hero.say(“This is a test.”)
becomes this:

``````hero.say("This is a test.")
``````
#11

okay, thank you now i understand

#12

actually I have found numbers of correct code on pages like these so…

#13

while True:
enemies = self.findEnemies()
enemy = self.findNearestEnemy()
defeated=0
if enemy:
self.attack(enemy)
defeated+1
if hero.time > 15:
self.moveXY(59, 33)
self.say(defeated)
break
while True:
items = self.findItems()
item = self.findNearest(items)
if item:
self.moveXY(item.pos.x, item.pos.y)
if hero.time>30:
self.moveXY(58, 33)
self.say(self.gold)
break

while True:
enemies = self.findEnemies()
enemy2 = self.findNearestEnemy()
defeated2=0
if enemy2:
self.attack(enemy2)
defeated2+1
if self.time > 45:
self.moveXY(59, 33)
self.say(defeated2)
break
this is my code, but for some reason the defeated and defeated2 variables always come out as 0
I am using Python

#14

Did you catch what @Hellenar said about beautiful formatting

1 Like
#15

I doubt you are even in this chat anymore but take out the `defeated2 = 0.` that is messing up the point that every time you add 1 to defeated2 it resets to 0.