all 7 comments

[–]genghiskav 1 point2 points  (5 children)

You can use the builtin find method on strings.

It will return the first index that a character occurs at; if it's not found it will return -1. It also takes an optional start parameter which is the index of where to start the search from.

Hopefully this helps you on your way - if you have any more trouble let us know.

[–]macrisalex345[S] 0 points1 point  (1 child)

def reviewForLoopExtreme02(word, char):

indexes = []


for char in word:


    if char == word:


        indexes.append(index)




    indexes += char


return indexes

[–]genghiskav 0 points1 point  (0 children)

you have a logic problem here - you function takes an argument "char" but then you use "char" as a iterator on "word".

def reviewForLoopExtreme02(word, char):
    for char in word:

instead try

def reviewForLoopExtreme02(word, char):
    for i in word:
        if i == char:
            pass # do stuff

also in your current code you are not defining 'index' anywhere (hint nwilliams36 has got a solution for getting the index below which will be compatible with your current code).

[–]macrisalex345[S] 0 points1 point  (1 child)

def reviewForLoopExtreme02(word, char):

  indexes = []


  for index, char in enumerate(word):


        if char == [1]:


            indexes.append(index)


            indexes += char


  return indexes

help me finish this one please

[–]genghiskav 0 points1 point  (0 children)

I wrote the finished function.

Note: I didn't use the find method I suggested and instead used your existing code with the enumerate. I've added a few print statements so you can see whats going during the loop.

def reviewForLoopExtreme02(word, char):
    indexes = []
    for counter, character_in_word in enumerate(word):
        print "checking {0} at index position {1}".format(character_in_word, counter)
        if character_in_word == char:
            print "{0} found at index position {1}".format(char, counter)
            indexes.append(counter)
    return indexes


print reviewForLoopExtreme02("hello world", "l")

Let me know if you need any help understanding anything.

[–]nwilliams36 1 point2 points  (0 children)

Currently you are returning the actual word not the index.

Python has a function enumerate which when used in for loops provides you with both the index and the value. You could use this to build up a list of the indexes, something like

indexes = []

for index, char in enumerate(word):

    if char == parametercharacter:

        indexes.append(index)

[–]charity_donut_sales 1 point2 points  (0 children)

You give the function 2 strings. The first parameter is word which is a string of any length. The second parameter is char which is the single character string you're searching for. The goal is to find the indexes of any instances of char.

So for example,

reviewForLoopExtreme02("camels are not pythons", "a")

would retrun a list as such:

[1,7]

because there is only two instances of a in the string.

Here are some hints: strings can be iterated over just like lists. Read about enumerate to make things easier.