[SOLVED] Help on Reindeer Wakeup

I can’t complete this level. It says the number of the reindeer, but it also says undefined.
Example: “Reindeer 0 is undefined”
Here is 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[deerIndex]="awake"
pass

while True:

for statusIndex in range(len(deerStatus)):
    # Tell Merek the reindeer index and its status.
    # Say something like "Reindeer 2 is asleep".
    #deerStatus[deerIndex]="asleep"
    hero.say("Reindeer " + statusIndex + "is " + deerIndex[statusIndex])

pass

Please help. Many thanks.

Hi @ILikeCoding Welcome the Discourse :partying_face: :tada:
Please format the code like the link shown below

Do I just copy and paste the code?

deerStatus = [ ‘asleep’, ‘asleep’, ‘asleep’, ‘asleep’, ‘asleep’ ]

friends = hero.findFriends()

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[deerIndex]="awake"
pass

while True:

for statusIndex in range(len(deerStatus)):
    # Tell Merek the reindeer index and its status.
    # Say something like "Reindeer 2 is asleep".
    #deerStatus[deerIndex]="asleep"
    hero.say("Reindeer " + statusIndex + "is " + deerIndex[statusIndex])

pass

You need to use preformatted text

# 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[deerIndex]="awake"
    pass
while True:
    
    for statusIndex in range(len(deerStatus)):
        # Tell Merek the reindeer index and its status.
        # Say something like "Reindeer 2 is asleep".
        #deerStatus[deerIndex]="asleep"
        hero.say("Reindeer " + statusIndex + "is " + deerIndex[statusIndex])
    
    pass

You have to find the reindeers again in the While loop by using the line below.

So like this?

# 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:
while true:
    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[deerIndex]="awake"
    pass
while True:
    
    for statusIndex in range(len(deerStatus)):
        # Tell Merek the reindeer index and its status.
        # Say something like "Reindeer 2 is asleep".
        #deerStatus[deerIndex]="asleep"
        hero.say("Reindeer " + statusIndex + "is " + deerIndex[statusIndex])
    
    pass

it still does nothing though.

Hi @ILikeCoding,
Actually you don’t want while True loops on either (at least, you don’t need them).
Look at this line (the only other error):
hero.say("Reindeer " + statusIndex + "is " + deerIndex[statusIndex])
Now, if you try it without the while loops it will probably say something like: “Reindeer 0is undefined”.
Well, let’s go from there. You’re using the variable you used in the first loop (for deerIndex in ran…). The reason it calls an error is because deerIndex is an integer (1, 2, 3) not an array (which is the only thing which can have stuff in it’s ).
So you’ve probably already realised you’re using the wrong variable. Where’s the array you want I wonder…
Danny

1 Like
# 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[deerIndex]="awake"
          
    pass

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

it is better, but the last deer never comes.

I believe you’re saying something like “Reindeer” and then the entire deerStatus array and then “is”, then the reindeer number.

1 Like
hero.say("Reindeer " + deerStatus[statusIndex] + " is asleep" )

I think what you want to say is:

hero.say("Reindeer " + statusIndex + " is" + " " + deerStatus[statusIndex])

Grzmot

And the reason for that is because statusIndex is the number you used in the for loop. It will be a number which increases from 0 - the number of reindeer (-1, because arrays start at index 0, so deerStatus[0] will be the status of reindeer number 1. Just to confuse you :wink:). So you use statusIndex to indicate the number reindeer you’re on, then you use statusIndex with deerStatus. (like grzmot said.).

# Let's say deer Status has been changed by the first for loop and it looks like this:
deerStatus = [ 'awake', 'asleep', 'asleep', 'awake', 'awake' ]
# and let's say you're on your first iteration (loop) of the second for loop, so statusIndex would be..
statusIndex = 0 # this is just an example not actually what you'd do.
# Then if you wanted 'awake' to be said, you'd put:
hero.say("Reindeer " + statusIndex + " is " + deerStatus[statusIndex])
# Which would output:

# >-- Reindeer 0 is awake

# Notice the extra space after the original " is", when you concatenate (add together) -
# - strings they don't create a space automatically, so when you put text inbetween  (like "is"), you need to make sure you have enough spaces.

I hope this helps you understand the problem, as well as beating the level.
Danny

ohhhhhhhhhhhh thanks so much. I appreciate the help!

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