Well, your code doesn’t have any indentation. (edit: you seem to have indentation, you just didn’t read the FAQ about how to insert your code here)
Python requires that you properly indent your code, because that’s how you mark code blocks. In other languages you use curly braces, end
tags, etc and don’t have to care much about indenting (although it still helps to maintain readabillity). But in python it’s essential. So your code should look something like:
loop:
friends = ...
# your code here
for friend in friends:
# this is your 'for' block
self.command(friend, "attack", enemy)
# your code here
# end of 'for' block
# end of 'loop' block
Furthermore, you need to get a better understanding of lists and objects:
self.findByType()
and self.findEnemies()
always returns a list, even if that list has only one element. To get a single object (e.g. your target) you need to pick an item (element, whatever) of that list. You can do it by using an index (e.g. list[x]
), or by using a function that returns a single value (e.g. self.findNearest(list)
):
enemies = self.findEnemies() # enemies = ['Ganju', 'Freesa', ...]
enemy = enemies[0] # enemy = 'Ganju'
nearest = self.findNearest(enemies) # nearest = 'Freesa'
Another issue is that for the for
loop you need a list to go through: for item in list
for example:
for enemy in enemies: # 'enemies' is a list of enemies
self.say(enemy) # say the current item (=enemy)
for coin in coins: # 'coins' is a list of coins
# do something with the current coin
I believe that these concepts are properly explained in some of the earlier levels. Amirite, @nick?
Please this post if you found it useful. Thanks!