[Solved] Please Help! Python Backwood Forest

Hello,
The task:
Defeat ogres
Collect 3 coins
Whey this code is not working?

while True:
enemy = hero.findNearestEnemy()
if enemy and enemy.type == “munchkin”:
hero.attack(enemy)
item = hero.findNearestItem()
if item and item.type==“coin”:
hero.attack(item)

The Loop runs only once.

Thanks for any advice

1 Like

First check your equipment to make sure all methods are doable.
Also, why are you trying to attack a coin? Just move to it. :wink:
In the future, be sure to format your code according to the FAQ using the </> button. :slight_smile:

thanks, but hero.attack(item) works only loop works once. Hero attacks enemy, collects coin but onlu once.

1 Like

Why are you attacking an item? Try hero.moveXY(coin.pos.x, coin.pos.y)
Have you checked equipment?

1 Like

it’s not a problem with attacking or moving to item. I need to defeat 3 ogres and collect 3 coins. with this code hero defeats 1 ogr and collects 1 coin and then loop breaks, and this is the problem. tusk must be made under 8 statemants,

1 Like

You cannot attack an item. The reason the loop breaks is because there is no function like that. Inside the for loop, you need to first check if “enemy” is valid, then attack them. Then, check if “item” is valid. If so,

hero.moveXY(coin.pos.x, coin.pos.y)
2 Likes

it works, ! finally got it! thanks for your support:)

2 Likes