What is wrong with my code, It seems OK, Zoo Keeper

# Защити клетку.
# Поставь солдата на каждую отметку Х.
points = []
points[0] = {"x": 33, "y": 42}
points[1] = {"x": 47, "y": 42}
points[2] = {"x": 33, "y": 26}
points[3] = {"x": 47, "y": 26}

# Собери 80 золота.
while True:
    
    while hero.gold <80:
        coin = hero.findNearest(hero.findItems ())
        hero.move(coin.pos)
    # Призови четырёх солдат.
    for i in range(4):
        hero.summon("soldier")
    
# Отправь своих солдат на позиции.
while True:
    friends = hero.findFriends()
    for j in range(len(friends)):
        point = points[j]
        friend = friends[j]
        enemy = friend.findNearestEnemy()
        if enemy and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
            # Прикажи войскам атаковать.
            hero.command(friend, "attack", enemy)
            pass
        else:
            # Прикажи войскам двигаться на точку.
            hero.command(friend, "move", point)
            pass
            

hi, not sure if a space is actually “allowed” there before the () part:

Other than that, are there any error messages or similar? Or is it doing something that it shouldn’t?

Additionally, you might want to check if “coin” exists afterwards before you try to actually move there.
If there’s no coin to move to, then you can’t “move” to it. (unsure if there’s always a coin, or not, if not it might cause your code to get stuck)

it doesn`t move to the next loop and shows error on that line, then I just used “break” and it worked also I have a question about these "j"s, please, take a look:

Level Noble Sacrifice

while hero.gold < 80:
    coin = hero.findNearest(hero.findItems ())
    hero.move(coin.pos)
# Построй 4 солдата-приманки
for i in range (4):
    hero.summon("soldier")
# Отправь своих солдат на позиции
points = []
points[0] = { "x": 13, "y": 73 }
points[1] = { "x": 51, "y": 73 }
points[2] = { "x": 51, "y": 53 }
points[3] = { "x": 90, "y": 52 }
friends = hero.findFriends()
for j in range (len (friends)):
    point = points [j]
    friend = friends [j]
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.command(friend, "move", point)
    # hero.command(friend, "move", point)
    
# Используй `range`, чтобы перебрать массив в цикле.
# Прикажи друзьям двигаться на нужные точки 

what are these [j] and how do they work??

for j in range (len (friends)):
    point = points [j]
    friend = friends [j]

let’s start with explaining a for loop then
range(len(friends)) would be the range between 0 and the length of friends
let’s say you have 5 friends, then it would be: 0,1,2,3,4
(yes, usually it starts with the 0)
so,
and for j in ...
the j in this part is a variable for the current iteration, so the first time it would be
points[0]
the second would be
points[1]
and as you don’t want to go “above” the number of friends you have, you set the range between
0 and the length of the friends array. so it would be 5 iterations.

so after each iteration the for loop automatically increases by 1 (or should if im not mistaken).
So you could actually just replace it with a while loop and increasing the index variable you declared at the end of it.

But on which line exactly does it throw an error?

Edit: I believe i forgot a crucial part… sorry :sweat_smile:
an array, you can think of it as a list.
So points[j] (let’s say it’s the first iteration, so j would be 0 in that particular case)
would refer to the very first entry in the array. (meaning points[0], which you declared further above as an object with an X and a Y variable)
You could (if there’s a set amount of friends) also do for example:

hero.command(friends[0], "move", points[0])
hero.command(friends[1], "move", points[1])
# and so on...

But as that would clutter the code even more with “unnecessary” code, as well as you can’t really be sure if you’ll lose a friend of those or even have them to begin with, it’s useful to use a while or for loop with a variable amount of iterations. So for example, at one moment you have 4 friends and the next moment you only have 3, or even 5.
Then, in case you got more, you’d not be commanding that one additional friend.
In case of 3, you’d get an error as you wouldn’t have the fourth friend anymore…
So a while or for loop is useful for these cases.


so, after i’ve probably given too much information and also weirdly explained, i still hope that some of what i wrote will help. :sweat_smile:
If you find it hard to understand my explanation, let me know! I’ll try to improve it in that case, as well as give detailed examples, if I’ll be able to :slight_smile:

2 Likes