Help! I don’t know how to loop an array can anyone help me?Please?(please put it in python)
How to actually loop over an array:
enemies = self.findEnemies() # Returns an array of enemies friends = self.findFriends() # Returns an array of friends shamans = self.findByType("shaman") # Returns an array of shamans # range(start, end) produces a list of numbers ranging from start (Inclusive!) to end (Exclusive!) # the following line will start with 0 and count up to len(enemies)-1 for i in range(0, len(enemies)): enemy = enemies[i] #do something with the enemy, like attacking
for is another type of loop than
loop will never stop to run. It will run until someone from the outside stops it (like the gameworld is ending, the real world is ending our you close the browser, for example).
for is a loop that will count up a counter to a value. When it reaches the value, it will stop. The advantage for you is that once all enemies are taken care of, your code continues after the
for-loop. While this may not seem useful in the first moment, it will soon become one of your most used tools as soon as you mastered it.
In a lot of programming languages indeces start at
0 and go to
len(array)-1. There are numerous reasons for that which I will not explain here because it breaks the context of the question.
Just remember that the 1. Element is at index 0, the 20. Element is at index 19 ect.
Thanks alot i passed