# Help with BookKeeper. Am i cheating?

Ok, i´m not sure i´m learning what i´m supossed to in this case.
My code works, but i failed to use a single break method(?)(command?). Besides, i had to use redundant variables.
What am i doing wrong??

``````defeated = 0

while self.now() <= 15:
enemy = self.findNearestEnemy()
if not enemy:
self.moveXY(34, 32)
continue
if enemy:
self.attack(enemy)
if enemy.health <=0:
defeated += 1

self.moveXY(59, 33)
self.say("I've defeated "+ defeated +" enemies")

while self.now() > 15 and self.now() <30:

coin = self.findNearestItem()

if not coin:
self.moveXY(34, 32)
if coin:
self.moveXY(coin.pos.x, coin.pos.y)

self.moveXY(59, 33)
self.say("I've collected " + self.gold + " coins")
defeated = 0
# Fight enemies until the clock reaches 45 seconds.
while self.now() > 30 and self.now() < 45:
enemy = self.findNearestEnemy()

if not enemy:
self.moveXY(34, 32)
continue
if enemy:
self.attack(enemy)
if enemy.health <=0:
defeated += 1

self.moveXY(59, 33)
self.say("I've defeated "+defeated+" enemies")

``````

It looks good to me, don’t know why you would need breaks for those loops because they automatically break with time anyway.

What are you referring too when you say redundant variables?

Edit:

I see, i went back to the level and apparently you’re supposed to use break.

So i guess you need to keep the while loops as `while True:`

try putting

``````if hero.now() > 15:
break
``````

inside the while loop

Well, that´s exactly what i tried to do, to no avail. The only way i could make it work was constructing those while loops.

But even then i had to cheat, you see. Let me explain:
those self.moveXY(34, 32) you surely read in my code are not there because i wanted to keep my character in a place, no. I HAD to put them there because otherwise the code would enter an endless loop.

Additionally, in every loop i also had to declare(?)(it´s that the correct term?) the “enemy” variable or else the interpreter(?)(Again? i REALLY need to look up these concepts) could not run it. “defeated” i could place on top of the file, “enemy”? not so much. Why is that?

interesting, somehow the while loop indeed behaves differently if you do it your way, even though I would expect it to behave the same.

it’s normal that you need to declare the enemy inside every loop, because the nearest enemy changes often, and if you declared it before, the enemy that you declared before is long dead.

Let’s say you see an enemy, and you declare

``````enemy = self.findNearestEnemy()
``````

so now enemy = enemy1 (the first enemy you encountered)

then you kill enemy1, and later in your code you call

``````self.attack(enemy)
``````

since the enemy has been killed a long time before, the code will get confused. because enemy1 has been killed long ago. Therefore, you need to constantly update the variable called enemy with fresh enemies, that’s perfectly normal.

The moving however is not, and it will be fixed if you use the while loop with the breaks.

May i see a sample code where you attempt to use break?

As you can probably guess, i did not keep the code. That was… careless of me.

Fear not! i shall try again with the break loops, the fruit loops and any other loops that dare place foot before me!, but not right now as i have to go to bed. Kinda sleepy, you know.

Again, thank you for your time.

1 Like