# Infinite loop in searven siege, help please!

**URL:** <https://discourse.codecombat.com/t/infinite-loop-in-searven-siege-help-please/4988>\
**Category:** Uncategorized\
**Created:** [September 20, 2015, 8:52am UTC](https://discourse.codecombat.com/t/infinite-loop-in-searven-siege-help-please/4988 "2015-09-20T08:52:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![imanflow](https://avatars.discourse-cdn.com/v4/letter/i/41988e/32.png) [@imanflow](https://discourse.codecombat.com/u/imanflow)\
**Post date:** [September 20, 2015, 8:52am UTC](https://discourse.codecombat.com/t/infinite-loop-in-searven-siege-help-please/4988/1 "2015-09-20T08:52:29Z")

</div>

I always try to solve all by myself but I am really struggling with this one: I really do not see why the error occurs. Here the code:

```
loop:
items=self.findItems()
itemsIndex=0
if items:
    while self.gold < 20:
        item=items[itemsIndex]
        if item:
            self.moveXY(item.pos.x, item.pos.y)
            itemsIndex += 1
if self.gold >= 20:
    count=1
    if count == 1:
        self.moveXY(83, 78)
        count =2
    else if count == 2:
        mself.moveXY(84, 51)
        count =3
    else if count ==3:
        self.moveXY(84, 22)
        count = 0

```

Thanks in advace!

---

<div class="post-metadata">

**Author:** ![mads232](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mads232](https://discourse.codecombat.com/u/mads232)\
**Post date:** [September 20, 2015, 9:10am UTC](https://discourse.codecombat.com/t/infinite-loop-in-searven-siege-help-please/4988/2 "2015-09-20T09:10:59Z")

</div>

What is the problem? You say infinite loop but you kinda wrapped the whole thing in a loop so it probably should be infinitely looping? 😄

Shouldn’t itemsIndex=0 be outside of the loop? I mean if it’s inside, wouldn’t it reset the itemsIndex back to 0 every time?

---

<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:** [September 20, 2015, 4:03pm UTC](https://discourse.codecombat.com/t/infinite-loop-in-searven-siege-help-please/4988/3 "2015-09-20T16:03:07Z")

</div>

Your indentation is a bit mixed up.  
Tip: Better use the Method in the FAQ to post code here, it also highlights the syntax.

Your `loop` is on the same line as the next one. Technically this means that you don’t order anything, forever.

Instead of

```python
loop:
items=...

```

do

```python
loop:
    items=...

```

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [September 21, 2015, 3:39pm UTC](https://discourse.codecombat.com/t/infinite-loop-in-searven-siege-help-please/4988/4 "2015-09-21T15:39:31Z")

</div>

Your other problem is here:

```python
items=self.findItems()
itemsIndex=0
if items:
    while self.gold < 20:
        item=items[itemsIndex]
        if item:
            self.moveXY(item.pos.x, item.pos.y)
            itemsIndex += 1

```

You go through the items array once (the items you can initially see at the beginning of the level), then you are done. If you can’t get 20 gold with those items, you’ll start looping forever. But not all the coins spawn at the very beginning of the level.

Probably you want to refresh `items = self.findItems()` each time through your while-loop, and then use `item = findNearest(items)` to get the right one to go after.

---

<div class="post-metadata">

**Author:** ![imanflow](https://avatars.discourse-cdn.com/v4/letter/i/41988e/32.png) [@imanflow](https://discourse.codecombat.com/u/imanflow)\
**Post date:** [October 8, 2015, 10:15am UTC](https://discourse.codecombat.com/t/infinite-loop-in-searven-siege-help-please/4988/5 "2015-10-08T10:15:01Z")

</div>

Thanks for your help and sorry for the late answer, I have been away.

I played further and learnt some new tricks.

Now all seems to work as intended, which is the main point. Running the code I succeeded but when I submitted it I lost hahaha I will try with better equipment

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [October 8, 2015, 2:29pm UTC](https://discourse.codecombat.com/t/infinite-loop-in-searven-siege-help-please/4988/6 "2015-10-08T14:29:36Z")

</div>

Please do not post correct code, for it gives other players an easy solution without providing education.

---

<div class="post-metadata">

**Author:** ![imanflow](https://avatars.discourse-cdn.com/v4/letter/i/41988e/32.png) [@imanflow](https://discourse.codecombat.com/u/imanflow)\
**Post date:** [October 8, 2015, 2:46pm UTC](https://discourse.codecombat.com/t/infinite-loop-in-searven-siege-help-please/4988/7 "2015-10-08T14:46:10Z")

</div>

OK, Editing right now!
