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

Uneasy Truce Python

#1

It’s showing me this strange error.
Here my code

def findSouthernUnits(units):
    southernUnits = []
    for unit in units:
        if unit.pos.y < hero.pos.y:
            # Add the unit to the array with: append()
            southernUnits.append(unit)
    return southernUnits


while True:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    # Use findSouthernUnits to get enemies to the south.
    findSouthernUnits()
    # If there are more ogres south of you than friends.
    if southernUnits > friends:
        
        # Then summon another "soldier".
        hero.summon("soldier")

and the capture of the error

0 Likes

#2

see additional comments:

while True:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    # Use findSouthernUnits to get enemies to the south.
    findSouthernUnits() # enemies to the south have to be put on a new array variable
                        # southernUnits = findSouthernUnits(units)
                        # where units are the enemies from hero.findEnemies()
    # If there are more ogres south of you than friends.
    if southernUnits > friends: # you compare two arrays and they're both objects 
                                # instead you must compare their length
                                # if len(array1) > len(array2) 
        # Then summon another "soldier".
        hero.summon("soldier")

As side note:
In code combat python implementation

if array1.length > array2.length: # is equal to
if len(array1) > len(array2): 

Learnt it just now :slight_smile:

1 Like

#3

Still don’t understand ;(
Truc

def findSouthernUnits(units):
    southernUnits = []
    for unit in units:
        if unit.pos.y < hero.pos.y:
            # Add the unit to the array with: append()
            southernUnits.append(unit)
    return southernUnits


while True:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    # Use findSouthernUnits to get enemies to the south.
    findSouthernUnits()
    southernUnits = findSouthernUnits(units) 
    # If there are more ogres south of you than friends.
    if len(southernUnits) > len(friends):
        
        # Then summon another "soldier".
        hero.summon("soldier")
0 Likes

#4

Hi,
The error is in this line:

What if there wasn’t a unit?
:lion: :lion: :lion:

0 Likes

#5

I have the same problem, can anyone solve it?

0 Likes

#6

Welcome @Coder101. Before you proceed in receiving assistance in your code, please be sure to tell us what problem you are having and the code you are currently using. When posting the code, use triple backticks(```).
This is what it should look like in the composer:

```

hero.say("Help me!)

```

And in the actual post, that will turn into this:

hero.say("It's formatted!")
0 Likes

#7
def findSouthernUnits(units):
    southernUnits = []
    for unit in units:
        if unit.pos.y < hero.pos.y:
            # Add the unit to the array with: append()
            southernUnits.append(unit)
    return southernUnits


while True:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    # Use findSouthernUnits to get enemies to the south.
    findSouthernUnits(1)
    # If there are more ogres south of you than friends.
    if southernUnits > len(friends):
        # Then summon another "soldier".
        hero.summon("soldier")
0 Likes

#8


This is a screenshot of the error

0 Likes

#9

I noticed that you are not providing an array for the function to check.

findSouthernUnits(1) #1 is not an array to run through the function

findSouthernUnits(enemies) #this 

Also, I assigned the findSouthernUnits() function to a variable to that receives the return southernUnits array.

ogres = findSouthernUnits(enemies)

Then compare the length of that variable (ogres) against the len(friends).

1 Like