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")
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):
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")
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:
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")
There’s a few problems that I can tell (even though I use Javascript):
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.