“Still does not work” does not give any information. Usually we do not have time or possibility to run your code. Even if we can run it we do not know what you actually intended to do.
1) If there is an error, tells us the error and mark the line in your code.
If it runs but do not do what you wanted, tell us
2) What are you seeing
3) what are you expecting to see
I’ll teach you to debug the code. Teach a man to finish, …
Get a white piece of paper and start with the first line.
The paper will mark everything that the code interpreter know
First line 1) loop – KNOWN: nothing, code interpreter does not know a thing
2) *enemy =self.findNearest(self.findEnemies())*
KNOWN: enemy - hero’s nearest enemy, possibly empty
Remember the possibly empty notation. Always assume that the values that you are getting are junk or latter in life, malicious inputs intended to break down your site or application
3) friend = self.findFriends()
enemy -hero’s nearest, possibly empty
friend - list of soldiers, possibly empty
4) (1 out of 2) if enemy:
During each if there a two paths to take: case when the condition is true and the case when the condition is false.
Each extra if adds another path. If there are 3 ifs one inside the other, the whole block has 4 different paths you need to consider
If you are taking a path, then you add the condition=true to the list of things the code interpreter knows at that time
This is inside the if so it means that enemy is not empty
if (value) is the standard way of checking if a value is not empty.
KNOWNS: nemy, hero’s nearest, *NOT empty
friends: list of soldiers, possibly empty
self.attack needs a not empty enemy value so it is ok
- (2 out of 2) There is no else branch for the if so we do nothing. When going over an “if” always count how many branches you need to go over and ask yourself: it is ok to ignore the missing branches?
In this case the branch is no enemy, and it may be ok for the hero to do nothing if there are no enemies around
Once you finish the if, you drop the knowledge from the if condition so for now on enemy can be empty again.
6) for soilder in soilders:
This will go over each soilder in the soilders list. It will be ok if the list is empty
KNOWS: enemy, heroes nearest, possibly empty (we are now outside the if so we have to drop the condition=true assumption.
friends - list of soliders, possibly empty
for soilder in soilders needs a soilders list-type value. The interpreter does not know it at the moment so BAM, your code fails.
7) self.command(soilder, "attack", enemy)
Once you fix the code and reach here,
KNOW: enemy, hero’s nearest, possibly empty
soilder - an item from the list soilders, not empty
friends - soldiers list, possibly empty
Soilder is know not empty because you got it from for soilder in beforehand.
if you will get the soliders list each loop using findFriends, then it will not contain empty value.
But on a more general case you could place an if(soilder) to make sure soilder is not empty
soilder - not empty
enemy - not empty
If you do not have any of this, then you’ll get an error again
Finally, the level asks your soldiers to attack their nearest enemy not yours. Their nearest enemy for a soldier soldy can be got with:
This requires soldy to be known, not empty.
The result soldy_nearest will be known but possibly empty