# 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.