[SOLVED] Help! Yeti Eater

It’s getting there. You’ve got the right amount of numbers in the for loop, but the first two aren’t right.
There are 6 yetis, and in the yetis array their numbers are:

yetis[0]
yetis[1]
yetis[2]
yetis[3]
yetis[4]
yetis[5]

Notice how the last number is 5 because the array started on 0? There are still 6 yetis. A normal for loop would go from 0 to the len(array) (in this case that would be 6:

for i in range(0, len(yetis)):
    yeti = yetis[i] # remember to define your yeti variable
    while yeti.health > 0:
        hero.attack(yeti)

In a normal for loop which uses range() and has two parameters (0, len(yetis)) the variable i goes up right? From 0-5 (remember for loops always go to one less than the second parameter in the for loop, as I said before, because arrays start at 0, so the amount of items in an array in normal numbers will be one higher than the number in the of an array.)
Also, in normal for loops a “step” of 1 upwards is always included. You don’t see it because it’s included in the normal for loop, but you can put it in like this:

for i in range(0, len(yetis), 1):
    yeti = yetis[i] # remember to define your yeti variable
    while yeti.health > 0:
        hero.attack(yeti)

It will do the exact same thing as the other code example.
If you want a for loop to go down by 1 each loop, what should you do? -1 is the answer. Now, you do have that, but think about the other numbers that you have: yetis() 0 (which isn’t really possible) and 10. That means that you’re going from an unidentified number upwards to 10. But that’s not right, we want to go down. -1 remember? So, you need to start on the highest element of the yetis array right? That will be the smallest yeti. Look back up at my list of elements, which one’s the highest? [5]. Written another way (and in a way that would still work if the number of yetis changed: len(yetis) - 1. Remember to include len(). Yetis is not a number by itself, len turns it into the number of yetis that there are: 6. This will work for all arrays, enemies, friends etc. You just need to minus 1 because of what we said above about arrays starting on 0.
Now, what do you want the loop to end on? The last element in the array! Which, because we’re going down :arrow_down: is [0].
As I said before, when looping in a for loop, the second parameter is one further than what you want to end on (In whichever direction the loop is going, up or down (that’s when the step is negative, like in our code)). If you were going up :arrow_double_up: and wanted to go from 0-9, you would use for i in range(0, 10) right? And if you were going down from 9-0, you’d want to use for i in range(9, -1, -1). Does that make sense? You’re subtracting 1 off 9 every loop until it becomes 0, and because the second parameter has to go one further than what you want to end up on, it will be -1.
Now read this:

I hope from reading my (rather long) post you will now understand why you need to use the parameters listed in the comments of the level. I hope this helped you understand it a little better.
Danny

3 Likes