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

[Help]: why is it okay not to use range() and len() here?

# Triage the wounded soldiers.

doctor = hero.findByType("paladin")[0]
mage = hero.findByType("pixie")[0]
helper = hero.findByType("peasant")[0]
soldiers = hero.findByType("soldier")

# Initialize patient arrays.
doctorPatients = []
magePatients = []
helperPatients = []

# Iterate all the soldiers:
for soldier in soldiers:
    # If soldier is slowed:
    if soldier.maxSpeed < 6:
        # Add them to the 'mage's array of patients.
        magePatients.append(soldier)
    # Else if soldier.health is less than half of maxHealth:
    elif soldier.health < soldier.maxHealth * 0.5:
        # Add them to the 'doctor's array of patients.
        doctorPatients.append(soldier)
    # Else:
    else:
        # Add soldier to the 'helper's array of patients.
        helperPatients.append(soldier)

# Now assign the patient lists to the appropriate person.
mage["patients"] = magePatients
doctor["patients"] = doctorPatients
helper["patients"] = helperPatients

Why didn’t they use something like this below?

for s in range(len(soldiers)):
    soldier = soldiers[s]
    if soldier.maxSpeed < 6:

The most puzzling thing to me is that, why do you have to use range() and len() for the string, just like the one in a different level. Just like this below.

def letterInWord(word, letter):
    for i in range(len(word)):
        character = word[i]
        # If character is equal to letter, return True
        if character == letter:
            return True
    # The letter isn't in the word, so return False
    return False

It gives me an error if I write something like this down here.

def letterInWord(word, letter):
    for character in word:
        if character == letter:
            return True
    return False

In short, I’ve seen it some times that some codes work with or without range() and len() function, when the others were only solved with the code having range(len(something)).
Please help.

I just checked it with real Python I just downloaded from the source homepage.

def letterInWord(word, letter):
    for i in word:
        if i == letter:
            return True
    return False

AND

def letterInWord(word, letter):
    for i in range(len(word)):
        character = word[i]
        if character == letter:
            return True
    return False

both of them gave the exact same results. Which means that the Code Combat shouldn’t discriminate the first one.

What levels are these, the original and where you found letterInWord()? And what error are you getting? I’d like to test out your question.

It may be related to the value being passed in to the function. Sometimes, the levels have limitations to reinforce specific methods or approaches to prevent using the same basic code for every situation. Other times it may be gear related or just require a certain level in the campaign to have access to the different styles.

It’s a very first def code from The Spy Among Us level.
https://codecombat.com/play/level/the-spy-among-us?
If I change the second code, letterInWord(), shown in my comment to the first one, I get an error massage that says “TypeError: Need an object”.

I see what you mean. This level prevents you from using a regular for loop to iterate through a string. Even cutting it down to a basic for loop with a new variable doesn’t work. I get the same error.

friend = "friends"
for i in friend:
    hero.say(i)

“TypeError: Need and Object”

1 Like

Thank you very much. I literally struggled with this like for over 4 days or so. :smiley: