10. Censor


#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 "*** *** ***".


I have 2 codes, one of them written using string(gives error) the other is list(works perfectly). I was wondering why string.join() function give error like this


Replace this line with your code. 
First code with using string;f
text="this hack is wack hack"
word="hack"
def censor(text,word):
    syndra=""
    text=text.split()
    for i in range(len(text)):
        if text[i]==word:
            syndra=syndra+ "*" * len(word)
        else:
            syndra=syndra+text[i]
    
    return " ".join(syndra)

print censor(text,word)

second code with using list;
text="naber olum manyak misin olum"
word="olum"
def censor(text, word):
    splitted = text.split()
    ben=[]
    for i in range(len(splitted)):
        if splitted[i] == word:
            ben.append(len(word)* "*")
        else:
            ben.append(splitted[i])
    return " ".join(ben)
print censor(text, word)


#2

.join() is to join a list into a string, but in your first code you already have a string

so you can leave out join, this presents the problem that you have no spaces between the words, how do you think you can fix this?

The list is the better solution


#4

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