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

Little Trouble /w Highlanders


This is computer science 5, lesson 13, “Highlanders.” I am having trouble with a part of my code. I’m not quite sure which part.

# You must defeat the ogres
# But they are using black magic!
# Only the highlander soldiers are immune.
# Find highlanders, their names always contain "mac"

highlanderName = "mac"

# This function should search for a string inside of a word:
def wordInString(string, word):
    lenString = len(string)
    lenWord = len(word)
    # Step through indexes (i) from 0 to (lenString - lenWord)
    for i in range(lenString - lenWord):
        # For each of them through indexes (j) of the word length
        j = (lenString - lenWord)[i]
            # If [i + j]th letter of the string is not equal [j]th letter of world, then break loop
        if [i + j] != [j]:
            # if [j]th is the last letter of the word (j == lenWord - 1), then return True.
    if [j] == lenWord - 1:
        return True
    # If loops are ended then the word is not inside the string. Return False.
    return False #∆ Remove this when the function is written.

# Look at your soldiers and choose highlanders only
soldiers = hero.findFriends()
for soldier in soldiers:
    if wordInString(, highlanderName):
        hero.say( + " be ready.")

I’ll help you, just need to write an answer, please wait a few minutes.
Thank you.


Firstly you don’t need to define j, it’s just like i, you instead need to put another for loop like the i one but with j, it’s a for loop inside a for loop; a little confusing I know.
Secondly the
if [i + j] != [j]:


needs some things before them, the [i + j] needs a array in front of it and so does [j] the arrays you need to put in front are the two ones in the brackets of your definition. (play around with which one to put before which other one.) The break is right.
I’m not sure about the [j] in the:

if [j] == lenWord - 1:
    return True

I think it could be right but take the brackets off for safety.

Hope this helps :grin:

If you want more advise join my clan ( and talk on the Hub (Kith Kin Guard Hub)
I’m trying to plug it everywhere!


Thanks! I’ll take a look at your clan.


What exactly do you mean by put an array in front of [i+j], er the using arrays in general.


For example: array[i+j] which would target the ith + the jth part of the array, in this case the array is word.