For and range function


#1

I m playing Cloudrip Mountain. I just finished Zoo keeper lv with the help of pre-existent code which introduce a new things without explanation. They are “i” and range() function.

for i in range(4):
    hero.summon("soldier")

for j in range(len(friends))

I am so confused that what are"i" and “j” in the code and what is range() function for? Why there are no explanation in the hints. It leads to another problem that I m stuck in the next level Noble Sacrifice which requires me to “Use range to make an array to loop over” I will be grateful for anyone can help.


#2

The i is like a variable. I believe everytime you loop through the function, it adds i + 1. So essentailly, the “range()” function is saying that as long as “i” is less than or “in range of” the number you pass which in this case is 4, continue to summon.


#3

It means everytime hero summon a soldier "i"will plus one until it reach 4? So hero summon 4 soldiers?


#4

Yes. (remember that i starts from 0) For example:

Summon One, i becomes 1

Summon the second one, i becomes 2

So in this level, you will summon 4 soldiers, you loop them and set them to a point (points[j]) and then if there is an enemy, you command each of them to attack it.


#5

and what is the meaning of

for j in range(len(friends)):
    point = point[j]
    friend = friends[j]

???


#6

I think it means one point per existing friend.


#7

The points or four corners are listed in an array. Looping through the array and saying point[j] is saying point[0], then point[1], then point[2]. Then you find all of your friends that you summoned and then say friends[0] to find the first friends and so on. Then, you command the first friend to move to the first point, friends[j].move(point[j]) and so on. Hope this helps


#8

Thank so much. I begin to understand but need time to digest , so glad that u can help me and so disappointed that the hints of the game didn’t explain it. Thank so much


#9

No problem. [20 Characters]


#10

Thanks for the feedback everyone :slight_smile:

I added a new hint to Zoo Keeper and Noble Sacrifice about for loops and range(). Hopefully that will help explain it a bit, but if you still find it confusing, let us know and we’ll make more improvements.