10, senior


#1

my code is:
def censor(text, word):
it = text.split()
lijst = ""
for i in range(0,len(it)):
if it[i] not in word:
lijst += it[i]
else:
lijst += len(word) * "*"

return lijst

print censor("this hack is wack hack", "hack")

the words my code returns aren't separated, does anyone know how i can do that????


#2


Potential solution




I took your code, letter for letter (or is it syntax for syntax?), and tweaked it until it worked (for me at least).
This is what I came up with:

def censor(text, word):
    it = text.split()
    lijst = ""
    for i in range(0,len(it)):
        if it[i] not in word:
            lijst += it[i] + " "
        else:
            count = len(word)
            #I manually placed the stars so that...
            while count > 0:
                lijst += "*"
                count -= 1
            #... I could then put a space after the 'censoring'
            else:
                lijst += " "
    #The following part deals with an issue I had with a space after the text
    lijst = lijst[0:len(lijst)-1]
    return lijst

Hope it helps.


#3

I did something similar, where each word added to the censored string was added with a " ". At the end, the last extra space is removed by censored[0 : len(censored) - 1] (returns the censored string but without the last index element, which is an extra space).

def censor(text, word):
    censored = ""
    text = text.split()
    for i in text:
        if i == word:
            censored = censored + "*" * len(i) + " "
        else:
            censored = censored + i + " "
    return censored[0 : len(censored) - 1]   #removes the last space in the censored string