[SOLVED] Help! Yeti Eater

# Yetis surround us and we need to defeat them.
# Luckily the wizard had time to cast the sleep spell.
# Your hero can devour the yetis' vital powers when they are defeated.
# Defeat them in the order from weakest to the strongest.

# The wizard sorted enemies, but in the order from the strongest to the weakest.
wizard = hero.findNearest(hero.findFriends())
yetis = wizard.findEnemies()

# You need iterate the yetis list in the reverse order with a 'for-loop'.
# The start value should be 'len(yetis) - 1'.
# Iterate while the index greater than -1.
# Use the negative step -1.

for i in (len(yetis)) -1:
    # Attack each enemy while its health greater than 0.
    for yeti in yetis:
        while yeti.health > 0:
            hero.attack(yeti)
    
    yetiIndex -= 1


My hero doesn’t do anything.
https://codecombat.com/play/level/yeti-eater?
Lydia

1 Like

I just killed the yetis straight on no matter the size you might get big sized which helps you.

I want to solve it the right way.
Lydia

3 Likes

I did yetis.reverse() , and not for i in (len(yetis)-1) becuse you just have to reverse the wizard’s list.

Lydia
20 characterssss

5 Likes

The big key to this level is to use the range() method with the len(yetis). The range allows you to chose the (start, stop, step) of a numerical pattern. Keep in mind that this will give you a number, and not an item in the list which makes you approach the yetis list differently using an index.

The specifics require you to do this in reverse order which can be done starting at the end, stopping at the beginning and stepping with negative number. Also realize that the stop will not run the number you have listed, but up to that number which confused me a few times.

1 Like

So like

for range(len(yetis) 0, 10, -1)

?
Lydia

1 Like

Close. It is a for loop so you need to have a variable connected to the loop.

for x in range():
    #then use the yetis list with the index number of x

Remember that when you step backwards, you need to start at the back and work your way forward. That is where the the len(yetis)-1 comes into play, giving you the last number as your start. It is counting how many yetis in the list then subtracting one since the index start at 0 and not 1. Now for the stop, how do you get the first number of the index to be used? Keep in mind that stop does not include the number you have in the range stop spot, but up to that number.

Example: range(0,10,1) = 0-9
To see this in code, you can add a simple few line and have your hero say it or use the console to understand how the range works.

for i in range(0,10,1):
    hero.say(i)
2 Likes

wizard = hero.findNearest(hero.findFriends())
yetis = wizard.findEnemies()

# You need iterate the yetis list in the reverse order with a 'for-loop'.
# The start value should be 'len(yetis) - 1'.
# Iterate while the index greater than -1.
# Use the negative step -1.


    # Attack each enemy while its health greater than 0.
for i in range(yetis) 0, 10, -1):
    while yeti.health > 0:
        hero.attack(yeti)
   

Now, I get an error.
Lydia

1 Like

you haven’t defined i yet I belive

I don’t think that is the case, because before in Desert or something, you don’t need to define i.
Lydia

1 Like

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

So I could do something like

for i in range(5, len(yetis), -1):
    yeti = yetis[i]
    while yeti.health > 0
        hero.attack(yeti)

?
Lydia

2 Likes

Hi Lydia,

Almost! The for loop format is:

for i in range(start, stop, step change):

In this case:
Your start point should be 1 less than the length of the array (see @Deadpool198’s post for the reasoning).
Your stop point should be 1 less than 0 (ditto).
Your step change is -1.

I like the way you keep going at a problem, and keep asking lots of questions until you solve it. We’ve all been there (it’s frustrating at times), and in the long run it will make you really good at programming (or whatever else you decide you want to learn) :grin:.

Jenny

3 Likes
for i in range(6, -1, -1):
    yeti = yetis[i]
    while yeti.health > 0
        hero.attack(yeti)

Like this?
Lydia

1 Like

Really close!

Think about the code running. i will be set to 6, so yeti will be set to yetis[6]. Which yeti is that? (Remember the stronget yeti is yetis[0]).

Jenny

1 Like

yetis[6] is going to be the weakest or is that yetis[5] ?
Lydia

Try both start points in the for loop if you’re not sure :wink:

1 Like

Thanks Danny and Jenny and brooksy125 for helping me out!
Lydia

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.