# Second Gem Help!

**URL:** <https://discourse.codecombat.com/t/second-gem-help/32695>\
**Category:** Level Help\
**Created:** [April 18, 2022, 9:38pm UTC](https://discourse.codecombat.com/t/second-gem-help/32695 "2022-04-18T21:38:15Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![CrunchyCode](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/crunchycode/32/25873_2.png) [@CrunchyCode](https://discourse.codecombat.com/u/CrunchyCode)\
**Post date:** [April 18, 2022, 9:38pm UTC](https://discourse.codecombat.com/t/second-gem-help/32695/1 "2022-04-18T21:38:15Z")

</div>

![Web capture_18-4-2022_143438_codecombat.com](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/1/6/162c0dab095d6eec951c8809d1a8dc04bbabebbc.jpeg)  
I keep trying to put in items[2] but it comes up witht this message even though I have defined items[2] to cordinates.

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [April 19, 2022, 12:46am UTC](https://discourse.codecombat.com/t/second-gem-help/32695/2 "2022-04-19T00:46:38Z")

</div>

OwO use `.pos` and remove the 6th line

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [April 19, 2022, 12:47am UTC](https://discourse.codecombat.com/t/second-gem-help/32695/3 "2022-04-19T00:47:00Z")

</div>

And remember that list indexes start from 0

---

<div class="post-metadata">

**Author:** ![CrunchyCode](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/crunchycode/32/25873_2.png) [@CrunchyCode](https://discourse.codecombat.com/u/CrunchyCode)\
**Post date:** [April 19, 2022, 8:40pm UTC](https://discourse.codecombat.com/t/second-gem-help/32695/4 "2022-04-19T20:40:46Z")

</div>

Still the same thing

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [April 19, 2022, 9:05pm UTC](https://discourse.codecombat.com/t/second-gem-help/32695/5 "2022-04-19T21:05:04Z")

</div>

Can you send your new code please?

---

<div class="post-metadata">

**Author:** ![CrunchyCode](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/crunchycode/32/25873_2.png) [@CrunchyCode](https://discourse.codecombat.com/u/CrunchyCode)\
**Post date:** [April 19, 2022, 9:12pm UTC](https://discourse.codecombat.com/t/second-gem-help/32695/6 "2022-04-19T21:12:07Z")

</div>

```python
while True:
    items = hero.findItems()
    gem = hero.findItems()
    items = []
    gem = []
    
    items[1] = "gem[2]"
    
    
    items.pos = "{x: 20, y: 35}"
    # If the length of items is greater or equal to 2:
    if len(items) >= 2:
        
        hero.move(items[1])
    # Else:
    else:
        hero.moveXY(40, 34)
        # Move to the center mark.
        
    

```

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [April 19, 2022, 9:36pm UTC](https://discourse.codecombat.com/t/second-gem-help/32695/7 "2022-04-19T21:36:20Z")

</div>

Remove lines 3-10 and do `hero.move(items[1].pos)` not `hero.move(items[1])`

---

<div class="post-metadata">

**Author:** ![CrunchyCode](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/crunchycode/32/25873_2.png) [@CrunchyCode](https://discourse.codecombat.com/u/CrunchyCode)\
**Post date:** [April 19, 2022, 10:42pm UTC](https://discourse.codecombat.com/t/second-gem-help/32695/8 "2022-04-19T22:42:56Z")

</div>

still the same thing ☹

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [April 20, 2022, 1:42am UTC](https://discourse.codecombat.com/t/second-gem-help/32695/9 "2022-04-20T01:42:32Z")

</div>

Post your code please…

---

<div class="post-metadata">

**Author:** ![Watamelon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/watamelon/32/43741_2.png) [@Watamelon](https://discourse.codecombat.com/u/Watamelon)\
**Post date:** [April 20, 2022, 9:06am UTC](https://discourse.codecombat.com/t/second-gem-help/32695/10 "2022-04-20T09:06:31Z")

</div>

First, you are defining the `items` to a blank list which result your hero don’t do anything. Second, you only need 1 array for `items`, because gems are `items`. Third, `items` is an array, you cannot just move into an array’s positions.

Try this:

```python
while True:
    items = hero.findItems()
    if len(items) >= 2: # When there are 2 or more gems...
        item = items[1] # Remember the second thing for array is '1'

        #Collect the Gem! Use moveXY()
    else:
        #Move to the center mark!

```

.  
**ITERATING AN ARRAY**  
An array is basically a list that stores things, where you need a “index” to iterate (also called loop) through it.  
The number inside a square bracket is called a “index”, where it helps you iterate through an array, for example:

```python
while True:
    enemies = hero.findEnemies()
    index = 0 # The index always start at 0
    while index < len(enemies): # While the index is less than the array enemies
        enemy = enemies[index] # Define the 'index' element enemies to 'enemy'
        while enemy.health > 0: # The reason to do this is to keep focus on this target, if you do not check the health, the hero will attack every enemy in the array only ONCE
            hero.attack(enemy)
        index += 1 # Add index by 1, so you can kill the next enemy
    # When there are no enemies, the while loop will finish and reset the index to 0 and find a new array of enemies if there are.

```

---

<div class="post-metadata">

**Author:** ![Watamelon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/watamelon/32/43741_2.png) [@Watamelon](https://discourse.codecombat.com/u/Watamelon)\
**Post date:** [April 20, 2022, 9:14am UTC](https://discourse.codecombat.com/t/second-gem-help/32695/11 "2022-04-20T09:14:13Z")

</div>

Your explanation is kind of incorrect, move only move 1 step, so he will only move once then go back to center again. Which will get him stuck

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [April 20, 2022, 11:08am UTC](https://discourse.codecombat.com/t/second-gem-help/32695/12 "2022-04-20T11:08:12Z")

</div>

No, because it checks if there is an item, and if there ISN’T then it moves back

---

<div class="post-metadata">

**Author:** ![CrunchyCode](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/crunchycode/32/25873_2.png) [@CrunchyCode](https://discourse.codecombat.com/u/CrunchyCode)\
**Post date:** [April 21, 2022, 10:04pm UTC](https://discourse.codecombat.com/t/second-gem-help/32695/13 "2022-04-21T22:04:51Z")

</div>

Still doesn’t work I think it might be a bug because I’ve tried everything and it keeps pulling up that message.

---

<div class="post-metadata">

**Author:** ![Watamelon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/watamelon/32/43741_2.png) [@Watamelon](https://discourse.codecombat.com/u/Watamelon)\
**Post date:** [April 22, 2022, 9:40am UTC](https://discourse.codecombat.com/t/second-gem-help/32695/14 "2022-04-22T09:40:19Z")

</div>

```python
while True:
    items = hero.findItems()
    if len(items) >= 2: # When there are 2 or more gems...
        item = items[1] # Remember the second thing for array is '1'

        #hero.moveXY(item.pos.x, item.pos.y)
    else:
        #Move to the center mark!

```

Do moveXY(pos.x, pos.y) like in the sample code.

---

<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:** [April 22, 2022, 9:40pm UTC](https://discourse.codecombat.com/t/second-gem-help/32695/15 "2022-04-22T21:40:28Z")

</div>

Closed on April 22, 2022, 9:40pm UTC

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