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.
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!
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.
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:
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
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.