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


#1

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


#2

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


#3

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


#4

I can help a little. Google translated it wrong.

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


#5

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

Sorry for my Google translate :slight_smile:

написал:


#6

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


#7

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:

#8
# 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)

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 :slight_smile:


#9

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 работает? )


#10

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 :slight_smile:


#11

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)


#12

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


#13

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


#14

Thanks to all

Nice to talk to smart people