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


#1



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


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


how do I get the spaces in there. help.


def censor(text,word):
    censoredtext = ""
    splittext = text.split()
    for item in splittext:
        if item == word:
            for item2 in splittext:
                if item2 != " ":
                    censoredtext = "*" + censoredtext
                else:
                    censoredtext = " " + censoredtext
        else:
            censoredtext = word + censoredtext
    return censoredtext


#2

You should not worry about spaces at this moment. For censor("hey qwe rty","hey") your function returns heyhey***. Lack of spaces is a minor problem.

Rethink your function, maybe even start from scratch. Deal with the bigger problems first.


But to answer your question, to add spaces you should use += " ".


#3

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