# \[HELP\] Resource Valleys - Cloudrip

**URL:** <https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069>\
**Category:** Uncategorized\
**Created:** [April 1, 2017, 10:55pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069 "2017-04-01T22:55:32Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![JorgeXDXD](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@JorgeXDXD](https://discourse.codecombat.com/u/JorgeXDXD)\
**Post date:** [April 1, 2017, 10:55pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/1 "2017-04-01T22:55:32Z")

</div>

Well… I was playing with Phyton but I can’t beat this level.  
I don’t know what’s wrong, so I hope you can help me \<3

Here’s my code:

> def commandPeasant(peasant, coins):  
> # Command the peasant to the nearest of their array  
> distance = 0  
> for i in coins:  
> coin = coins[i]  
> if distance == 0:  
> distance = peasant.distanceTo(coin)  
> item = coin  
> elif distance \< peasant.distanceTo(coin):  
> distance = peasant.distanceTo(coin)  
> item = coin
> 
> ```
> hero.command(peasant, "move", item.pos)
> 
> ```

> friends = hero.findFriends()  
> peasants = {  
> “Aurum”: friends[0],  
> “Argentum”: friends[1],  
> “Cuprum”: friends[2]  
> }

> while True:  
> items = hero.findItems()  
> goldCoins =   
> silverCoins =   
> bronzeCoins =   
> for item in items:  
> if item.value == 3:  
> goldCoins.append(item)  
> # Put bronze and silver coins in their approriate array:  
> if item.value == 2:  
> silverCoins.append(item)  
> if item.value == 1:  
> bronzeCoins.append(item)  
> commandPeasant(friends[0], goldCoins)  
> commandPeasant(friends[1], silverCoins)  
> commandPeasant(friends[2], bronzeCoins)

---

<div class="post-metadata">

**Author:** ![Reflective\_Key](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Reflective\_Key](https://discourse.codecombat.com/u/Reflective_Key)\
**Post date:** [April 27, 2018, 11:20pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/2 "2018-04-27T23:20:52Z")

</div>

I’m struggling with this level too, but can you tell me if what (if any) errors pop up.

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [April 28, 2018, 12:49am UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/3 "2018-04-28T00:49:51Z")

</div>

this was one year ago.

copy and past you’re code properly please

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [April 28, 2018, 1:49am UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/4 "2018-04-28T01:49:00Z")

</div>

You can revive an old post as long as it’s not off topic

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [April 28, 2018, 6:21pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/5 "2018-04-28T18:21:32Z")

</div>

he might have found the answer by one year.

---

<div class="post-metadata">

**Author:** ![SuperSmacker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/supersmacker/32/8385_2.png) [@SuperSmacker](https://discourse.codecombat.com/u/SuperSmacker)\
**Post date:** [April 30, 2018, 12:45am UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/6 "2018-04-30T00:45:10Z")

</div>

> [@Reflective\_Key](#):
>
> I’m struggling with this level too, but can you tell me if what (if any) errors pop up.

It’s not about the original person’s code. It’s about @Reflective_Key’s code.

---

<div class="post-metadata">

**Author:** ![freesat-106](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/freesat-106/32/8241_2.png) [@freesat-106](https://discourse.codecombat.com/u/freesat-106)\
**Post date:** [June 21, 2018, 10:55pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/7 "2018-06-21T22:55:51Z")

</div>

Could someone please help me? I can’t get the peasant to move to the coins.  
Here is part my code:

> function commandPeasant(peasant, coins) {  
> // Command the peasant to the nearest of their array  
> hero.command(peasant, “move”, coins.pos);  
> }

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [June 21, 2018, 11:30pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/8 "2018-06-21T23:30:46Z")

</div>

if item.value == 3:  
goldCoins.append(item)  
if item.value == 2:  
silverCoins.append(item)  
if item.value == 1:  
The above construction may work by pure luck, but generally is wrong. Do it the right way

```python
if item.value == 3:
    goldCoins.append(item)
elif item.value == 2:
    silverCoins.append(item)
elif item.value == 1:
    bronzeCoins.append(item)

```

this I can see from the broken format

---

<div class="post-metadata">

**Author:** ![freesat-106](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/freesat-106/32/8241_2.png) [@freesat-106](https://discourse.codecombat.com/u/freesat-106)\
**Post date:** [June 21, 2018, 11:36pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/9 "2018-06-21T23:36:45Z")

</div>

but i’m using JavaScript.

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [June 22, 2018, 8:19am UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/10 "2018-06-22T08:19:46Z")

</div>

same code in javascript:

```python
        if(item.value == 3) goldCoins.push(item);
        else if(item.value == 2) silverCoins.push(item);
        else if(item.value == 1) bronzeCoins.push(item);

```

in your code read carefully the hint:  
// Command the peasant to the **nearest of their array**

```python
function commandPeasant(peasant, coins) {
// You must get here a single coin from coins and tell the peasant to move there
// coins.pos is an array of points , not a single coin.pos 
hero.command(peasant, “move”, coins.pos); // wrong
}

```

---

<div class="post-metadata">

**Author:** ![freesat-106](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/freesat-106/32/8241_2.png) [@freesat-106](https://discourse.codecombat.com/u/freesat-106)\
**Post date:** [June 22, 2018, 9:10pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/11 "2018-06-22T21:10:02Z")

</div>

So how would I command the peasant to the nearest of their array? Do I do something like this?

> function commandPeasant(peasant, coins) {  
> // Command the peasant to the nearest of their array  
> var coin = coins[0];  
> hero.command(peasant, “move”, coin.pos);  
> }

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [June 22, 2018, 9:17pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/12 "2018-06-22T21:17:26Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/7/72571a6b8b9676734e6819f9a20206e9fa7f88c0.png)  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a6bab9820ce221669c2f8743122ca2e542e98e77.png)  
the first picture was a hint  
the second is from [Thang editor](https://codecombat.com/editor/thang/peasant-m)  
see all API of thangs: [https://codecombat.com/editor/thang/](https://codecombat.com/editor/thang/)

---

<div class="post-metadata">

**Author:** ![freesat-106](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/freesat-106/32/8241_2.png) [@freesat-106](https://discourse.codecombat.com/u/freesat-106)\
**Post date:** [June 22, 2018, 9:28pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/13 "2018-06-22T21:28:07Z")

</div>

Sorry to bother you, but is this how I should be doing this?

> hero.command(peasant, “move”, peasant.findNearest(peasant.findItems));

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [June 22, 2018, 9:37pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/14 "2018-06-22T21:37:23Z")

</div>

peasant has no peasant.findItems method. / and if he has it will be peasant.findItems()/  
Your function is commandPeasant(peasant, coins), where you have the array **coins** as parameter.  
So it will be:

```python
function commandPeasant(peasant, coins) {
    var coin = peasant.findNearest(coins);
    hero.command(peasant, “move”, coin.pos); 
}

```

---

<div class="post-metadata">

**Author:** ![freesat-106](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/freesat-106/32/8241_2.png) [@freesat-106](https://discourse.codecombat.com/u/freesat-106)\
**Post date:** [June 22, 2018, 9:52pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/15 "2018-06-22T21:52:21Z")

</div>

I did what you said, but now it says: "TypeError: Cannot read property ‘pos’ of null "

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [June 22, 2018, 10:01pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/16 "2018-06-22T22:01:45Z")

</div>

somewhere you must have

```python
var coins = hero.findItems()

```

to pass coins to commandPeasant(peasant, coins). A check

```python
if (coin && peasant ) hero.command(peasant, “move”, coin.pos);

```

will get rid of the error "Cannot read property ‘pos’ of null ", but I think you have a logical error.

---

<div class="post-metadata">

**Author:** ![freesat-106](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/freesat-106/32/8241_2.png) [@freesat-106](https://discourse.codecombat.com/u/freesat-106)\
**Post date:** [June 22, 2018, 10:05pm UTC](https://discourse.codecombat.com/t/help-resource-valleys-cloudrip/11069/17 "2018-06-22T22:05:35Z")

</div>

It worked! Thank you very much! 😀
