Uneasy Truce Python

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

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:

3 Likes

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")

Hi,
The error is in this line:

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

I have the same problem, can anyone solve it?

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!")
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")


This is a screenshot of the error

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

There’s a few problems that I can tell (even though I use Javascript):

  1. The parameter or whatever you put in the brackets in the ```findSouthernUnits(1)```` shouldn’t be one. Imagine plugging that into the function (or def) with 1 as units. This is how it would look like:
def findSouthernUnits(1):
    southernUnits = []
    for unit in 1:
        if unit.pos.y < unit.pos.y:
            southernUnits.append(unit)
    return southernUnits

It wouldn’t do anything as 1 is a value, not an array of objects. Try replacing that with enemies.
2. You haven’t defined what southernUnits is. Tip: it is equal to findSouthernUnits(enemies)
3. On this line: if southernUnits > len(friends):, only one of them is a value of the array’s length. You must compare both their lengths.

The problem with your code is that you are comparing your southernUnits to the length of your friends array. You should try comparing the length of findSouthernUnits to the length of your friends array.