[Solved] Help with Wild horses python

I can’t solve Wild horses. I’ve been working at it for a while. It said "cannot read x of undefined. " here is my code:
while True:
# How do you find the nearest friendly unit?
# horse = ?
horse = hero.findFriends()
if horse:
x1 = horse.pos.x - 7
x2 = horse.pos.x + 7

    if x1 >= 1:
        # Move to the horse's y position but use x1 for the x position.
        hero.moveXY(x1,horse.pos.y)
    elif x2 <= 79:
        # Move to the horse's y position but use x2 for the x position.
        hero.moveXY(x2,horse.pos.y)
    distance = hero.distanceTo(horse)
    if distance <= 10:
        hero.say("Whoa")
        # Move to the red x to return the horse to the farm
        hero.moveXY(27,54)
        # Move back out into the pasture to begin looking for the next horse.
        hero.moveXY(43,33)
1 Like

Please make sure that ALL of your code is pasted in between the triple back ticks or only some of it will be formatted properly.

oh,sorry. Fixed.

while True:
    # How do you find the nearest friendly unit?
    # horse = ?
    horse = hero.findFriends()
    if horse:
        x1 = horse.pos.x - 7
        x2 = horse.pos.x + 7
        if x1 >= 1:
            # Move to the horse's y position but use x1 for the x position.
            hero.moveXY(x1,horse.pos.y)
        elif x2 <= 79:
            # Move to the horse's y position but use x2 for the x position.
            hero.moveXY(x2,horse.pos.y)
        distance = hero.distanceTo(horse)
        if distance <= 10:
            hero.say("Whoa")
            # Move to the red x to return the horse to the farm
            hero.moveXY(27,54)
            # Move back out into the pasture to begin looking for the next horse.
            hero.moveXY(43,33)

The error code is correct. You have defined the variable, horse, as an array. Then, in your code, you are looking for a particular horse and its position. You have not defined a single horse so it can only see an array of horses.

Your code is very close. Once you correct the variable definition it should work.

Thank you. Solved!:grin:

please help me my code is

while True:
    # How do you find the nearest friendly unit?
    # horse = ?
    horse = hero.findFriends()
    if horse:
        x1 = horse.pos.x - 7
        x2 = horse.pos.x + 7
        if x1 >= 1:
            # Move to the horse's y position but use x1 for the x position.
            hero.moveXY(x1,horse.pos.y)
        elif x2 <= 79:
            # Move to the horse's y position but use x2 for the x position.
            hero.moveXY(x2,horse.pos.y)
        distance = hero.distanceTo(horse)
        if distance <= 10:
            hero.say("Whoa")
            # Move to the red x to return the horse to the farm
            hero.moveXY(27,54)
            # Move back out into the pasture to begin looking for the next horse.
            hero.moveXY(43,33)

findFriends, gives you an array of all of your friends, but you have to find the nearest one.

1 Like

oh ok thank you so much(20 char)

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

I can’t figure out why this isn’t working.

while True:
    # How do you find the nearest friendly unit?
    # horse = ?
    horse = hero.findNearestFriend()
    
    
    
    if horse:
        x1 = horse.pos.x - 7
        x2 = horse.pos.x + 7
        
        if x1 >= 1:
            # Move to the horse's y position but use x1 for the x position.
            hero.moveXY(x1, horse.pos.x)
        elif x2 <= 79:
            # Move to the horse's y position but use x2 for the x position.
            hero.moveXY(x2, horse.pos.y)
        distance = hero.distanceTo(horse)
        if distance <= 10:
            hero.say("Whoa")
            # Move to the red x to return the horse to the farm.
            hero.moveXY(28, 54)
            # Move back out into the pasture to begin looking for the next horse.
            hero.moveXY(44, 30)
            

Neither horse = hero.findNearestFriend() or horse = hero.findFriends() work.

@RangerGrant9307, I am here.

xD. Well, x1 and x2 are x coordinates. hrose.pos.x is also x coordinate. :joy: You tried to move xy to 2 x coordinates. See the problem?

ahem My new code:

while True:
    # How do you find the nearest friendly unit?
    # horse = ?
    horse = hero.findNearestFriend()
    
    
    
    if horse:
        x1 = horse.pos.x - 7
        x2 = horse.pos.x + 7
        
        if x1 >= 1:
            # Move to the horse's y position but use x1 for the x position.
            hero.moveXY(x1, horse.pos.y)
        elif x2 <= 79:
            # Move to the horse's y position but use x2 for the x position.
            hero.moveXY(x2, horse.pos.y)
        distance = hero.distanceTo(horse)
        if distance <= 10:
            hero.say("Whoa")
            # Move to the red x to return the horse to the farm.
            hero.moveXY(28, 54)
            # Move back out into the pasture to begin looking for the next horse.
            hero.moveXY(44, 30)
            

My top line gets an error saying Type Error.

horse = hero.findNearestFriend() doesn’t exist.
Use hero.findNearest(hero.findFriends())

Thanks! This worked too once I added horse = hero.findNearest(hero.findFriends())

did u pass da level

1 Like

I sure did. Thanks again.

mark as solution plz

She can’t cause this is not her topic.

grant_is_stupid