Censor indices problem


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


I set my index variable at 0 with the intent to increase it by 1 with each iteration. For some reason, the index reaches 1 and then will not go any higher.

Error message: Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** hey hey" when it should return "*** *** ***".


I want my "index" variable to increase by 1 with each loop iteration, that way my split[key] call will replace the appropriate word.


def censor(text, word):
    split = text.split()
    print split
    for phrase in split:
        index = 0
        if phrase == word:
            split[index] = "*" * len(phrase)
        index = index + 1
        print index
    new_text = " ".join(split)
    return new_text
    
censor("I am your Father", "your")


#2

Define this outside of the loop.


#3

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