# Проблема на уровне "Песчаные змеи"

**URL:** <https://discourse.codecombat.com/t/topic/8267>\
**Category:** русский (Russian)\
**Created:** [July 3, 2016, 5:41pm UTC](https://discourse.codecombat.com/t/topic/8267 "2016-07-03T17:41:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![e1se](https://avatars.discourse-cdn.com/v4/letter/e/76d3ee/32.png) [@e1se](https://discourse.codecombat.com/u/e1se)\
**Post date:** [July 3, 2016, 5:41pm UTC](https://discourse.codecombat.com/t/topic/8267/1 "2016-07-03T17:41:01Z")

</div>

![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/8/8147434cd56ef4a97a90aa54496df234b91f42f1.png)

В месте где лежит персонаж, у меня он срезает угол. Хотя должен был его обойти.  
В чём может быть проблема?

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [July 3, 2016, 7:44pm UTC](https://discourse.codecombat.com/t/topic/8267/2 "2016-07-03T19:44:22Z")

</div>

I do no speak Russian but I will try to help if I can.  
( нет я не говорю на русском, но я буду стараться помочь, если я могу. )

This is Python. In Python “tabs” matter  
( Это Python. В Python “закладках” материя )

`if a < 1 [tab]b = 2`

is different from  
( отличается от )

`if a \< 1  
b = 2’

Recheck your [tab] 's  
перепроверьте [tab] 's

![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex016/uploads/codecombat/original/2X/6/69c199d62a566cefd0480ad7b1fe497938e6d1b7.png)

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [July 3, 2016, 7:47pm UTC](https://discourse.codecombat.com/t/topic/8267/3 "2016-07-03T19:47:49Z")

</div>

If you are programming in Python this might help in the future. Use comments:  
( Если вы программируете в Python это может помочь в будущем. Используйте комментарии: )

```
while coinIndex < len(coins):
    coin = coins[coinIndex]
#while

```

[google translate is cool](https://translate.google.com/#en/ru/If%20you%20are%20programming%20in%20Python%20this%20might%20help%20in%20the%20future.%20%20Use%20comments%3A)

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [July 4, 2016, 5:44am UTC](https://discourse.codecombat.com/t/topic/8267/4 "2016-07-04T05:44:01Z")

</div>

> [@Harry\_the\_Wanderer](#):
>
> This is Python. In Python “tabs” matter

I can help a little. Google translated it wrong.

В питоне отступы очень важны и влияют на порядок выполнени команд.

---

<div class="post-metadata">

**Author:** ![e1se](https://avatars.discourse-cdn.com/v4/letter/e/76d3ee/32.png) [@e1se](https://discourse.codecombat.com/u/e1se)\
**Post date:** [July 4, 2016, 7:42am UTC](https://discourse.codecombat.com/t/topic/8267/5 "2016-07-04T07:42:55Z")

</div>

the problem occurs only in one place, where there is a character in the  
screenshot

Sorry for my Google translate 🙂

написал:

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [July 4, 2016, 8:02am UTC](https://discourse.codecombat.com/t/topic/8267/6 "2016-07-04T08:02:06Z")

</div>

Пожалуйста, выложите код, так мы сможем помочь быстрее, чем пытаться вычитать код на скриншоте. Не забывайте только про форматирование кода (тройные `)

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [July 4, 2016, 8:43am UTC](https://discourse.codecombat.com/t/topic/8267/7 "2016-07-04T08:43:23Z")

</div>

e1se: Right. Your if is part of the while loop.

**Delete 1 tab from the last if.**

```
# If there's a nearest
if nearest:

```

vs.

```
 # If there's a nearest
     if nearest:
```

---

<div class="post-metadata">

**Author:** ![e1se](https://avatars.discourse-cdn.com/v4/letter/e/76d3ee/32.png) [@e1se](https://discourse.codecombat.com/u/e1se)\
**Post date:** [July 4, 2016, 12:59pm UTC](https://discourse.codecombat.com/t/topic/8267/8 "2016-07-04T12:59:10Z")

</div>

```
# This field is covered in firetraps. Thankfully we've sent a scout ahead to find a path. He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.

# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
loop:
    coins = hero.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    
    # Loop through all the coins to find the nearest one.
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        nearest = coins[coinIndex]
        distance = self.distanceTo(coin)
        # If this coin's distance is less than the nearestDistance
        if distance < nearestDistance:
            # Set nearest to coin
            nearest = coin
            # Set nearestDistance to distance
            nearestDistance = distance
            
        # If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
        if nearest:
            self.moveXY(nearest.pos.x, nearest.pos.y)

```

 ![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a481ddccba0e7a17913e4771422296e0635730c2.png)

After the character approach to the coin number 1, he immediately goes to the coin number 2 pass over the average coin

Sorry for google translate 🙂

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [July 4, 2016, 5:59pm UTC](https://discourse.codecombat.com/t/topic/8267/9 "2016-07-04T17:59:18Z")

</div>

The google translate is ok!  
(Google Translate нормально!)

Which is correct?  
( Что правильно )

```
while coinIndex < len(coins):
    if nearest:
        self.moveXY(nearest.pos.x, nearest.pos.y)

```

or

```
while coinIndex < len(coins):
if nearest:
    self.moveXY(nearest.pos.x, nearest.pos.y)

```

Should we be checking `nearest:` inside the `while` loop or outside the `while` loop?  
( Должны ли мы проверить `nearest:` внутри `while` во время или вне `while` в то время? )

Hopefully my google translate worked?  
( Надеюсь, мой Google Translate работает? )

---

<div class="post-metadata">

**Author:** ![e1se](https://avatars.discourse-cdn.com/v4/letter/e/76d3ee/32.png) [@e1se](https://discourse.codecombat.com/u/e1se)\
**Post date:** [July 4, 2016, 6:22pm UTC](https://discourse.codecombat.com/t/topic/8267/10 "2016-07-04T18:22:57Z")

</div>

I think that:

```
while coinIndex < len(coins):
    if nearest:
        self.moveXY(nearest.pos.x, nearest.pos.y)

```

is correct, but code don’t worked

We checking nearest: inside the while loop

_ **P.S. Without google tranlate 🙂** _

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [July 4, 2016, 6:57pm UTC](https://discourse.codecombat.com/t/topic/8267/11 "2016-07-04T18:57:39Z")

</div>

Challenge: Trace the while loop to verify:  
( Задача: Проследить то время цикла для проверки: )

coinIndex = 0  
coins = [array\_of\_items]  
nearest = None  
nearestDistance = 9999

**`<loop 1>`**

coin = coins[0]  
coinIndex = 1  
nearest = coins[1]  
distance = ?

coin’s distance is less than the nearestDistance = ? (true / false ) ?  
nearest = ?  
nearestDistance = ?

.

**`<loop 2>`**

?

.

**`<loop 3>`**

?

* * *

Hint:  
( намек )

`hero.say(distance)`

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [July 5, 2016, 6:08am UTC](https://discourse.codecombat.com/t/topic/8267/12 "2016-07-05T06:08:09Z")

</div>

Блок `if nearest:` идет на том же уровне что и `if distance < nearestDistance:`. То есть по сути вы будете проверять существование nearest после на каждую монету и пытаться идти к ней. а должно быть после цикла while.

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [July 5, 2016, 8:14am UTC](https://discourse.codecombat.com/t/topic/8267/13 "2016-07-05T08:14:58Z")

</div>

Thanks Bryukh. I was trying to explain how to debug the code.

---

<div class="post-metadata">

**Author:** ![e1se](https://avatars.discourse-cdn.com/v4/letter/e/76d3ee/32.png) [@e1se](https://discourse.codecombat.com/u/e1se)\
**Post date:** [July 5, 2016, 3:31pm UTC](https://discourse.codecombat.com/t/topic/8267/14 "2016-07-05T15:31:31Z")

</div>

_ **`Thanks to all`** _

Nice to talk to smart people
