Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED]Zoo Keeper Python Help with For loop

I have been trying to get through zoo keeper, but I don’t really get the for loop.Please help.
Here is my code.

# Protect the cage.
# Put a soldier at each X.
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}

# 1. Collect 80 gold.
items = hero.findItems()
for item in items:
    hero.move(item.pos)
# 2. Build 4 soldiers.
for i in range(4):
    hero.summon("soldier")
    
# 3. Send your soldiers into position.
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:
            # Command friend to attack.
            hero.command(friend, "attack", enemy)
            
            
        else:
            # Command friend to move to point.
            hero.command(friend, "move", points)
            

Instead of points put point.

Delete this part of the code.
Does it work now?

1 Like

Not really, my hero just tries to go to all the items at once, so I think there is a problem with the

while hero.gold < 80: # you're collecting coins non stop, just do this
    hero.move(hero.findNearest(hero.findItems()).pos)
# A for loop is basically a simpler way to write a while loop:
for x in y:
    hero.say(x) # hero will say starting from 0 all the way to infinity

Ok thanks, it works now.

In some ways, it is. But please don’t tell everyone to use it. There are some things a while loop can do which a for loop can’t.
For loops are good for looping a specified amount of times, like

for i in range(0, len(array)):
    print(i)

While loops are good for looping for an unspecified amount of time:

input alarmCheck
while alarmCheck == False:
    print("All going well!")
print("SOUND THE ALARM!!!")

A for loops can’t do the second example.
Danny