# Targeting enemy types

**URL:** <https://discourse.codecombat.com/t/targeting-enemy-types/17541>\
**Category:** Level Help\
**Created:** [February 19, 2019, 3:29pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541 "2019-02-19T15:29:33Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trea\_Wright](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Trea\_Wright](https://discourse.codecombat.com/u/Trea_Wright)\
**Post date:** [February 19, 2019, 3:29pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/1 "2019-02-19T15:29:34Z")

</div>

I’m doing Backwoods treasure in the forest. I’m having trouble targeting one enemy type. Here’s may code. (First time posting so sorry if my formatting is off.)

```python
while True:
    item = hero.findNearestItem()
    enemy1 = hero.findNearestEnemy()
    enemies = hero.findEnemies()
    enemyCount = 0
    for enemy in enemies:
        distance = hero.distanceTo(enemy)
        if distance < 10:
            enemyCount = enemyCount + 1
    if enemy1:
        if enemy1.type == "ogre" | enemy1.type == "scout":
            hero.attack(enemy1)
            hero.attack(enemy1)
            hero.attack(enemy1)
        else:
            hero.attack(enemy1)
    if enemyCount > 5:
        hero.cleave(enemy1)
    if item:
        hero.moveXY(item.pos.x, item.pos.y)
    else:
        hero.moveXY(40, 33)

```

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 19, 2019, 3:52pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/2 "2019-02-19T15:52:26Z")

</div>

First, what level are you on. Second. Can you explain what your code is supposed to do and how

> [@Trea\_Wright](#):
>
> ```python
> enemy1 = hero.findNearestEnemy() 
> enemies = hero.findEnemies() 
> enemyCount = 0 for enemy in enemies: 
> distance = hero.distanceTo(enemy) 
> if distance &lt; 10: enemyCount = enemyCount + 1 if enemy1: 
> if enemy1.type == "ogre" | enemy1.type == "scout": 
> hero.attack(enemy1) 
> hero.attack(enemy1)
> hero.attack(enemy1)
> 
> ```

works. I am a little confused on how this code works. Also a screenshot would be helpful.

-@Luke10

---

<div class="post-metadata">

**Author:** ![ducky](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ducky/32/9364_2.png) [@ducky](https://discourse.codecombat.com/u/ducky)\
**Post date:** [February 19, 2019, 9:01pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/3 "2019-02-19T21:01:30Z")

</div>

> [@Trea\_Wright](#):
>
> if enemy1.type == “ogre” | enemy1.type == “scout”:

Is the problem. Try `and`.

---

<div class="post-metadata">

**Author:** ![Trea\_Wright](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Trea\_Wright](https://discourse.codecombat.com/u/Trea_Wright)\
**Post date:** [February 20, 2019, 2:36pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/4 "2019-02-20T14:36:10Z")

</div>

I’m on Backwoods treasure. I set “enemy1” to the nearest enemy. Then I set all enemies I can see as “enemies”. The I set a float variable called “enemyCount” and I set it to zero. the I create another variable called “distance” which is set to the distance from me to the enemy. In a for loop I make it to where if distance is less than five then increase “enemyCount” by 1. Then I have been trying to use a nested if statement to attack scouts and ogres before going back to pick up items. I’ll get back to you with the screenshot.

---

<div class="post-metadata">

**Author:** ![Trea\_Wright](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Trea\_Wright](https://discourse.codecombat.com/u/Trea_Wright)\
**Post date:** [February 20, 2019, 2:40pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/5 "2019-02-20T14:40:32Z")

</div>

Sorry, but that doesn’t quite work. But with re-reading it I didn’t fully explain the problem. I want to target those enemy types and attack them multiple times while I attack anything else once. I still attack those enemies but I only attack once as if they were normal enemies.

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 20, 2019, 2:43pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/6 "2019-02-20T14:43:31Z")

</div>

> [@Luke10](#):
>
> a screenshot would be helpful.

(twenty Characters )

---

<div class="post-metadata">

**Author:** ![Trea\_Wright](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Trea\_Wright](https://discourse.codecombat.com/u/Trea_Wright)\
**Post date:** [February 20, 2019, 3:07pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/7 "2019-02-20T15:07:14Z")

</div>

![com-video-to-gif](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/8/83e7cd50b98d886a9ba7ed7741c7273b8067073a.gif)  
There’s a gif of the level.

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 20, 2019, 5:12pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/8 "2019-02-20T17:12:55Z")

</div>

You need to start over. I think there that @ducky was right about

> [@ducky](#):
>
> > if enemy1.type == “ogre” | enemy1.type == “scout”:
> 
> Is the problem. Try `and` .

but if that doesn’t work you need to start over

---

<div class="post-metadata">

**Author:** ![Trea\_Wright](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Trea\_Wright](https://discourse.codecombat.com/u/Trea_Wright)\
**Post date:** [February 20, 2019, 6:49pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/9 "2019-02-20T18:49:37Z")

</div>

Like re-write the entire code?

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 20, 2019, 7:21pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/10 "2019-02-20T19:21:42Z")

</div>

Well you already know from what you explained to me about what your code does, the basics of what needs to happen. Also if you reset it, You will still have the comments. I honestly think you should. If you need any more help, you can post again on this topic or message me personally and I’ll be happy to help!

-@Luke10

---

<div class="post-metadata">

**Author:** ![Eugene\_Ang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eugene_ang/32/9841_2.png) [@Eugene\_Ang](https://discourse.codecombat.com/u/Eugene_Ang)\
**Post date:** [March 16, 2019, 12:01pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/11 "2019-03-16T12:01:42Z")

</div>

Hi can you pm me and also send me your items?  
Happy to help.  
@Eugene_Ang

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [March 17, 2019, 2:20pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/12 "2019-03-17T14:20:39Z")

</div>

In codecombat, it is not possible for someone to transfer inventory to some else. You can either earn gems to buy certain items by completing levels, or buy either subscription that codecombat offers and you will receive and new character, gems, and XP every month.

-@Luke10  
🦊🦊🦊

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [March 17, 2019, 2:39pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/13 "2019-03-17T14:39:14Z")

</div>

I think @Eugene_Ang meant: “can you send a screenshot / list of the items”, not the physical game items.  
🦁 🦁 🦁

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [March 17, 2019, 3:21pm UTC](https://discourse.codecombat.com/t/targeting-enemy-types/17541/14 "2019-03-17T15:21:42Z")

</div>

Ohhh. ok! Sorry @Eugene_Ang if that’s what you meant. Thanks @Deadpool198

(Please dont edit my posts for no reason moderators (Chaboi\_300 specifically)
