Censor program returns 'None'


#1



Hey everyone, I'm stuck at the 'Censor' exercise in 'Practice Makes Perfect'


The error that I'm running into is that my program returns 'None' and I'm really at a loss to understand why. When I look at lots of other examples of loops, it's not as though each one 'returns' every single value. When do I know when I should type 'return' or how exactly to do the for-loop with an if-statement.


For starters, I expected this to:

  1. Change the word into a collection of asterisks that were the same number as the length of the word.

  2. Then split the string given in the 'text' portion of the function.

  3. Then for each string in the split text, if one of the strings is equal to the word, it would get the index of that string, and then replace the string with the censor word.

  4. Then it turns the list with the replaced word into a string.

  5. Then it joins it all together at the end and returned the joined version of the text.


def censor(text,word):
    censor_word = '*' * len(word)
    split_string = text.split()
    for item in split_string:
        if item == word:
            split_ind = split_string.index(item)
            new_text = split_string.insert(split_ind, censor_word)
            new_str = str(new_text)
            joined = "".join(new_str)
            return joined


#2

you are not replacing, you are inserting, that is a huge difference

if you want to update an item in a list using index you can simply do:

aList[index] = "new value"

#3

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