# \[SOLVED\] Decoy Drill

**URL:** <https://discourse.codecombat.com/t/solved-decoy-drill/30918>\
**Category:** Level Help\
**Created:** [October 11, 2021, 3:21pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918 "2021-10-11T15:21:31Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dae\_Shaun\_High\_Hawk](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dae_shaun_high_hawk/32/21794_2.png) [@Dae\_Shaun\_High\_Hawk](https://discourse.codecombat.com/u/Dae_Shaun_High_Hawk)\
**Post date:** [October 11, 2021, 3:21pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/1 "2021-10-11T15:21:31Z")

</div>

i Really cannot figure this one out i’ve been trying for about 20 days now  
it keeps saying “empty elif statement put 4 spaces into the elif” Im not sure what i did wrong  
any suggestions?

```python
decoysBuilt = 0
hero.gold = 0

while True:
    item = self.findNearestItem()
# Loot the coin!
x = item.pos.x
y = item.pos.y
hero.moveXY(x, y)
# Each decoy costs 25 gold. Use the Quartz Sense Stone
# to know when you have more than 25 gold with self.gold.
hero.gold += item.value
if hero.gold >=25:
    hero.buildXY("decoy", hero.pos.x - 5,hero.pos.y)
    # Keep a count of decoys you built as you go along.
    decoysBuilt = decoysBuilt +1
    # Break out of the loop when you have built 4.
elif decoysBuilt == 4:
        

```

---

<div class="post-metadata">

**Author:** ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)\
**Post date:** [October 11, 2021, 3:55pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/2 "2021-10-11T15:55:28Z")

</div>

Welcome back @Dae_Shaun_High_Hawk, I believe you have 3 problems in your code.

- you should indent everything once after this line for them to all be in the `while-true loop`

> [@Dae\_Shaun\_High\_Hawk](#):
>
> `item = self.findNearestItem()`

- also, the following line is irrelevant as you can not change the value of gold unless you collect coins, you should probably remove it

> [@Dae\_Shaun\_High\_Hawk](#):
>
> `hero.gold += item.value`

- about the error you are getting, you don’t have anything after this statement:

> [@Dae\_Shaun\_High\_Hawk](#):
>
> `elif decoysBuilt == 4:`

so you should type in something or remove this line fully.

---

<div class="post-metadata">

**Author:** ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)\
**Post date:** [October 11, 2021, 3:56pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/3 "2021-10-11T15:56:40Z")

</div>

> [@Dae\_Shaun\_High\_Hawk](#):
>
> `decoysBuilt = decoysBuilt +1`

and an easier way to do this is

```python
decoysBuilt += 1

```

---

<div class="post-metadata">

**Author:** ![Dae\_Shaun\_High\_Hawk](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dae_shaun_high_hawk/32/21794_2.png) [@Dae\_Shaun\_High\_Hawk](https://discourse.codecombat.com/u/Dae_Shaun_High_Hawk)\
**Post date:** [October 11, 2021, 3:58pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/4 "2021-10-11T15:58:32Z")

</div>

it also says now after i did that, that it says i cannot write to protected property gold

---

<div class="post-metadata">

**Author:** ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)\
**Post date:** [October 11, 2021, 3:59pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/5 "2021-10-11T15:59:02Z")

</div>

Can you screenshot or explain a bit more? And also please post your new code

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [October 11, 2021, 3:59pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/6 "2021-10-11T15:59:08Z")

</div>

> [@Dae\_Shaun\_High\_Hawk](#):
>
> ```python
> # Loot the coin!
> x = item.pos.x
> y = item.pos.y
> hero.moveXY(x, y)
> # Each decoy costs 25 gold. Use the Quartz Sense Stone
> # to know when you have more than 25 gold with self.gold.
> hero.gold += item.value
> if hero.gold >=25:
> hero.buildXY("decoy", hero.pos.x - 5,hero.pos.y)
> # Keep a count of decoys you built as you go along.
> decoysBuilt = decoysBuilt +1
> # Break out of the loop when you have built 4.
> elif decoysBuilt == 4:
>         
> 
> ```

First you need to replace the while true loop with a while loop. While decoysBuilt is less than four, you need to run the rest of the code. You need to indent all of this so that it is within the while loop, otherwise this piece of code will never run.

> [@Dae\_Shaun\_High\_Hawk](#):
>
> ```python
> item = self.findNearestItem()
> # Loot the coin!
> x = item.pos.x
> y = item.pos.y
> 
> ```

Before defining x and y as the item’s position x and y, you need to check if that item exists.

> [@Dae\_Shaun\_High\_Hawk](#):
>
> `hero.gold += item.value`

You can delete this statement.

> [@Dae\_Shaun\_High\_Hawk](#):
>
> ```python
> elif decoysBuilt == 4:
>         
> 
> ```

You need to check if the decoysBuilt is equal to four. If it is equal to four, then break out of the loop using break.

---

<div class="post-metadata">

**Author:** ![Dae\_Shaun\_High\_Hawk](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dae_shaun_high_hawk/32/21794_2.png) [@Dae\_Shaun\_High\_Hawk](https://discourse.codecombat.com/u/Dae_Shaun_High_Hawk)\
**Post date:** [October 11, 2021, 4:01pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/7 "2021-10-11T16:01:09Z")

</div>

@Aya

 ![Screenshot 2021-10-11 11.00.44 AM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/b/8/b87756e41f104312bc9a0abd9dbb3f4873c241be.jpeg)

---

<div class="post-metadata">

**Author:** ![Dae\_Shaun\_High\_Hawk](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dae_shaun_high_hawk/32/21794_2.png) [@Dae\_Shaun\_High\_Hawk](https://discourse.codecombat.com/u/Dae_Shaun_High_Hawk)\
**Post date:** [October 11, 2021, 4:01pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/8 "2021-10-11T16:01:48Z")

</div>

like that ??

im not that good with the loops still haha

---

<div class="post-metadata">

**Author:** ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)\
**Post date:** [October 11, 2021, 4:04pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/9 "2021-10-11T16:04:31Z")

</div>

You should indent the `if-statement` and what’s inside it because they are currently outside the `while-true loop` also you should add at the end inside the `if-statement` a “break” so that it exits the loop

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [October 11, 2021, 4:04pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/10 "2021-10-11T16:04:54Z")

</div>

Delete the `hero.gold = 0` statement. You don’t need that.

---

<div class="post-metadata">

**Author:** ![Dae\_Shaun\_High\_Hawk](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dae_shaun_high_hawk/32/21794_2.png) [@Dae\_Shaun\_High\_Hawk](https://discourse.codecombat.com/u/Dae_Shaun_High_Hawk)\
**Post date:** [October 11, 2021, 4:06pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/12 "2021-10-11T16:06:52Z")

</div>

```python
decoysBuilt = 0
while True:
    item = hero.findNearestItem()
    # Loot the coin!
    x = item.pos.x
    y = item.pos.y
    hero.moveXY(x, y)
    # Each decoy costs 25 gold. Use the Quartz Sense Stone
    # to know when you have more than 25 gold with self.gold.
    hero.gold += item.value
if hero.gold >=25:
    hero.buildXY("decoy", hero.pos.x - 5,hero.pos.y)
    # Keep a count of decoys you built as you go along.
    decoysBuilt +1
    # Break out of the loop when you have built 4.  
    break

```

---

<div class="post-metadata">

**Author:** ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)\
**Post date:** [October 11, 2021, 4:07pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/13 "2021-10-11T16:07:32Z")

</div>

> [@Dae\_Shaun\_High\_Hawk](#):
>
> `if hero.gold >=25:`

indent this line and everything afterwards, for them to be included in the loop

---

<div class="post-metadata">

**Author:** ![Dae\_Shaun\_High\_Hawk](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dae_shaun_high_hawk/32/21794_2.png) [@Dae\_Shaun\_High\_Hawk](https://discourse.codecombat.com/u/Dae_Shaun_High_Hawk)\
**Post date:** [October 11, 2021, 4:10pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/14 "2021-10-11T16:10:11Z")

</div>

> [@Dae\_Shaun\_High\_Hawk](#):
>
> if hero.gold \>=25:

```python
decoysBuilt = 0
while True:
    item = hero.findNearestItem()
    # Loot the coin!
    x = item.pos.x
    y = item.pos.y
    hero.moveXY(x, y)
    # Each decoy costs 25 gold. Use the Quartz Sense Stone
    # to know when you have more than 25 gold with self.gold.
    hero.gold += item.value
    if hero.gold >=25:
        hero.buildXY("decoy", hero.pos.x - 5,hero.pos.y)
    # Keep a count of decoys you built as you go along.
    decoysBuilt +1
    # Break out of the loop when you have built 4.  
    break

```

sorry for making this a hassle to have this stuff explained to me i am quite dumb sometimes haha

---

<div class="post-metadata">

**Author:** ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)\
**Post date:** [October 11, 2021, 4:12pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/15 "2021-10-11T16:12:01Z")

</div>

> [@Aya](#):
>
> and everything afterwards,

don’t forget this (20 chars)

---

<div class="post-metadata">

**Author:** ![Dae\_Shaun\_High\_Hawk](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dae_shaun_high_hawk/32/21794_2.png) [@Dae\_Shaun\_High\_Hawk](https://discourse.codecombat.com/u/Dae_Shaun_High_Hawk)\
**Post date:** [October 11, 2021, 4:12pm UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/16 "2021-10-11T16:12:38Z")

</div>

I figured it out now thanks @aya & @abc

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [October 12, 2021, 4:12am UTC](https://discourse.codecombat.com/t/solved-decoy-drill/30918/17 "2021-10-12T04:12:53Z")

</div>

Closed on October 12, 2021, 4:12am UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
