Practice makes Perfect 10/15


#1

What's wrong with my code:

def censor(text, word):
    array = text.split(" ")
    for x in array:
        if word == array[x]:
            array[x] = "*" * len(word)
    return " ".join(array)

#2

I added a few comments to your code, in order to fix it you will have to use enumerate or turn your array into a number sequence.

EXAMPLE:

for number, word in enumerate(array):
    print(number, word)

for number in range(len(array)):
    print(number)

Your CODE:

def censor(text, word):   # text = 'a word hurts me'  word = 'hurts'
    array = text.split(" ")   # array = ['a', 'word', 'hurts', 'me']
    for x in array:   # This will not work first loop x == 'a'
        if word == array[x]:   # array['a'] will not return anything and should throw a error
            array[x] = "*" * len(word)
    return " ".join(array)