# Need help in Drop The Flag

**URL:** <https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441>\
**Category:** Uncategorized\
**Created:** [November 24, 2014, 5:56pm UTC](https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441 "2014-11-24T17:56:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kannon](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@kannon](https://discourse.codecombat.com/u/kannon)\
**Post date:** [November 24, 2014, 5:56pm UTC](https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441/1 "2014-11-24T17:56:13Z")

</div>

I am stuck. This is my code not sure what to do. Any suggestions? Thanks

```python
# When you're not building traps, pick up coins!

loop:
    flag = self.findFlag()
    if flag:
        # How do we get fx and fy from the flag's pos?
        self.pickUpFlag(flag)
        fx, fy = 30, 47
        fx, fy = 30, 31

        self.buildXY("fire-trap", fx, fy)
        
    else:
        item = self.findNearestItem()
        if (item):
            pos = item.pos
            itemX = pos.x
            itemY = pos.y
            self.moveXY(itemX, itemY)

```

---

<div class="post-metadata">

**Author:** ![matt](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@matt](https://discourse.codecombat.com/u/matt)\
**Post date:** [November 24, 2014, 6:57pm UTC](https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441/2 "2014-11-24T18:57:45Z")

</div>

I’m guessing the issue is the question in the comment? If so the flag has a pos object to, i.e. you can do flag.pos.x

Does that help?

---

<div class="post-metadata">

**Author:** ![vishwathg](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@vishwathg](https://discourse.codecombat.com/u/vishwathg)\
**Post date:** [November 24, 2014, 10:13pm UTC](https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441/3 "2014-11-24T22:13:03Z")

</div>

flag.pos.x = the x-value of the position of the flag.  
flag.pos.y = the y-value of the position of the flag.

---

<div class="post-metadata">

**Author:** ![kannon](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@kannon](https://discourse.codecombat.com/u/kannon)\
**Post date:** [November 25, 2014, 8:01am UTC](https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441/4 "2014-11-25T08:01:26Z")

</div>

I tried doing flag.pos.x but it gave me error saying “fx is not define”. So I tried to define fx and fy but it is not working out.

self.pickUpFlag(flag)  
fx = flag.pos.x  
fy = flag.pos.y  
fx, fy = 30, 46  
fx, fy = 30, 32

```
    self.buildXY("fire-trap", fx, fy)
```

---

<div class="post-metadata">

**Author:** ![dwhittaker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dwhittaker/32/2114_2.png) [@dwhittaker](https://discourse.codecombat.com/u/dwhittaker)\
**Post date:** [November 25, 2014, 11:51am UTC](https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441/5 "2014-11-25T11:51:14Z")

</div>

Now you are defining fx to be the position, then you are assigning a new value of 30. Then you assign 30 to it again. Your code will always build the trap at (30, 32).

Delete the two fx, fy = ?, ? lines of code and it should work fine.

---

<div class="post-metadata">

**Author:** ![kannon](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@kannon](https://discourse.codecombat.com/u/kannon)\
**Post date:** [November 25, 2014, 8:34pm UTC](https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441/6 "2014-11-25T20:34:11Z")

</div>

Thanks very much. Work just as you say.

---

<div class="post-metadata">

**Author:** ![Titan](https://avatars.discourse-cdn.com/v4/letter/t/7ba0ec/32.png) [@Titan](https://discourse.codecombat.com/u/Titan)\
**Post date:** [February 2, 2015, 8:38pm UTC](https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441/7 "2015-02-02T20:38:31Z")

</div>

I tried this and it still dosen’t work. Do you know how to fix it? (code below)

```python
loop:
    flag = self.findFlag()
    if flag:
        itemII = self.findNearestItem()
        # How do we get fx and fy from the flag's pos?
        # (Look below at how to get x and y from items.)
        
        if itemII.type = "flag":
            flagPos = self.findNearestItem()
            fx = flagPos.x 
            fy = flagPos.y
            self.buildXY("fire-trap", x, y)
            self.pickUpFlag(flag)
    else:
        item = self.findNearestItem()
        if item:
            itemPos = item.pos
            itemX = itemPos.x
            itemY = itemPos.y
            self.moveXY(itemX, itemY)

```

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [February 2, 2015, 8:44pm UTC](https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441/8 "2015-02-02T20:44:43Z")

</div>

`if itemII.type = "flag":`  
You should use `==` instead.

`=` is an assignment.  
`==` is a comparison.

* * *

Also your code is wrong, though you were probably mislead by the comment about looking below.

`flag` already contains your flag. You can handle it like an item:

```python
loop:
    flag = self.findFlag()
    if flag:
        flagPos = flag.pos
        fx = flagPos.x 
        fy = flagPos.y
        self.buildXY("fire-trap", x, y)
        self.pickUpFlag(flag)
    else:
        item = self.findNearestItem()
        if item:
            itemPos = item.pos
            itemX = itemPos.x
            itemY = itemPos.y
            self.moveXY(itemX, itemY)

```

---

<div class="post-metadata">

**Author:** ![Titan](https://avatars.discourse-cdn.com/v4/letter/t/7ba0ec/32.png) [@Titan](https://discourse.codecombat.com/u/Titan)\
**Post date:** [February 2, 2015, 8:48pm UTC](https://discourse.codecombat.com/t/need-help-in-drop-the-flag/1441/9 "2015-02-02T20:48:01Z")

</div>

Thanks!!!, I hope it works now.
