Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Reindeer Wakeup - Python

I can’t complete this level. It calls out one reindeer, but then the last one doesn’t wake up. Here’s my code:

# This array contains the status for each reindeer.
deerStatus = [ 'asleep', 'asleep', 'asleep', 'asleep', 'asleep' ]

# And this array contains our reindeer.
friends = hero.findFriends()

# Loop through the reindeer and find the awake ones:
for deerIndex in range(len(friends)):
    reindeer = friends[deerIndex]

    # Reindeer with y position > 30 aren't in a pen.
    # If so, set the reindeer's entry to "awake".
if reindeer.pos.y > 30:
    deerStatus = "awake"
    pass

# Loop through statuses and report to Merek.
for statusIndex in range(len(deerStatus)):
    # Tell Merek the reindeer index and its status.
    # Say something like "Reindeer 2 is asleep".
    hero.say("Reindeer" + statusIndex + "is asleep")
    pass
1 Like

Double-check your indentations…as shown above, your for loop will complete, then the if statement will fire and finish and then the last for loops will run.

Hint/Spoiler

Notice where the # comments are aligned? Typically, this is where you want your code to line at, too

What do you mean by that?

1 Like

You need to indent the if reindeer code block.

It doesn’t seem to matter. Either way, it still does the same thing.

1 Like

Ok, now that the formatting should be correct, line 14…which deerStatus is equal to awake?

Edit: Just noticed…line 21, you should use the variable to announce the reindeer’s status, not just hard code it as “is asleep”.

There is only one deerStatus; the one on line 2.

1 Like

Take a look at line 2…there are actually 5 deerStatus elements; at line 14, you are iterating thru these, so you need to specify which one you are changing.

deerStatus is an array…how do you specify a specific element of an array?

I think it might be that I put if deer.pos.y > 30, not < 30

1 Like

no, that part is correct. I copied your code, fixed lines 14 and 21 and it completed fine.

For me, lines 14 and 21 are pass lines. I changed deerStatus[4] to become “awake”, and it is worse than I started with.

1 Like

Then you’ve changed something…removed a line somewhere. Copying your code from above, as posted by you, and fixing what used to be lines 14 and 21 is all it takes to make it work.

what do you mean by fix?

1 Like

You mentioned you changed line 14(?) to deerStatus[4]…that is fine, except it will always be referencing element # 4. What should you use instead?..remember, you are already iterating (looping through) ‘friends’, so you want to update the current friend’s status.

Line 21, you need to concatenate three items…"Reindeer " and statusIndex and deerStatus[statusIndex]…adding such spacing as to make it look nice, of course :wink:

This is my code, and it says, “Reindeer(#)undefined”.

# This array contains the status for each reindeer.
deerStatus = [ 'asleep', 'asleep', 'asleep', 'asleep', 'asleep' ]

# And this array contains our reindeer.
friends = hero.findFriends()

# Loop through the reindeer and find the awake ones:
for deerIndex in range(len(friends)):
    reindeer = friends[deerIndex]

    # Reindeer with y position > 30 aren't in a pen.
    # If so, set the reindeer's entry to "awake".
if reindeer.pos.y > 30:
    deerStatus = "awake"


# Loop through statuses and report to Merek.
for statusIndex in range(len(deerStatus)):
    # Tell Merek the reindeer index and its status.
    # Say something like "Reindeer 2 is asleep".
    hero.say("Reindeer" + statusIndex + deerIndex[statusIndex])
    pass
1 Like

you still haven’t fixed the indent problem…highlight (select) the two lines in the ‘if reindeer’ block and press tab. This will indent both lines one additional time, bringing them in to proper alignment.

Counting down from what you just posted (or by using the number line in the level), line 14, you need to specify which element of deerStatus should be changed to “awake”…you are using deerIndex as the counter for this for loop, so use it.

Line 21 looks good, but will be hard to read. hero.say("Reindeer " + statusIndex + " " + deerIndex[statusIndex]), would look better.

I did as you said in line 21, but it says “undefined” as the deerIndex[statusIndex]. I don’t get what you mean by “element of deerStatus”. Does that mean like I did earlier with deerStatus[4]?

1 Like

deerStatus = [ ‘asleep’, ‘asleep’, ‘asleep’, ‘asleep’, ‘asleep’ ] … this is an array that represents the status of each of the 5 reindeer. At the beginning of the code, they are all set to = ‘asleep’. The task is to change this, for those reindeer who are out of their stalls.

To start doing this, you use a for loop to iterate (loop through) the array of reindeer, called ‘friends’. At the start of the iteration, deerIndex is equal to zero, representing reindeer[0]. You then want to test to see if the reindeer[0] is above ‘y’ position 30 and if it is, change the corresponding deerStatus ([0] at this point) to equal ‘awake’…if it is not, leave as is, = ‘asleep’.

Once done processing friend[0], the code loops back (starting again), only now deerIndex = 1. It will test for friend[1]'s position and set the corresponding element as needed.

And so on, until all 5 friends have been processed.

Does this help clarify? If not, I’m out of hints and can only provide the fix, at this point.

Do I just repeat this for all five reindeer?

    for friend in friends:
        deerIndex = 0
        if reindeer.pos.y > 30:
            deerStatus[0] = "awake"
        else:
            deerStatus[0] = "asleep"
1 Like

No, that defeats the purpose of the lesson, which is to accomplish the task using arrays.