10Censor


#1



https://www.codecademy.com/en/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 "*** *** ***".


def censor(text,word):
    new=text.split(text)
    final=""
    for n in range(0,len(new)):
        if new[n]==word:
            new[n]="*"*len(word)
        else: 
            new[n]=new[n]
    final=new[n].join(final)
    return final
    print final
print censor("you are my sunshine","are")


I have been struggling on this exercise. Thank you and all help is appreciated :slight_smile:


#2

i would recommend you to check documentation how to use .join(), its where you go wrong


#3

Thanks, I give up using .join(). However it still have the same bug for some reason.

def censor(text,word):
    new=text.split(text)
    final=""
    for n in range(0,len(new)):
        print new[n]
        if new[n]==word:
            new[n]="*"*len(word)
        #replace the word with"*"
        else: 
            new[n]=new[n]
        #stay the same value
        final+=new[n]
        #add them all together 
    return final
    print final
print censor("you are my sunshine","are")

#4

but you need to use .join(), given you have to join new into a string


#5

I see what you mean, but still get the same error :frowning:
Sorry I am still confused.

def censor(text,word):
    new=text.split(text)
    experiment=[]
    final=""
    for n in range(0,len(new)):
        print new[n]
        m="*"*len(word)
        if new[n]!=word:
            experiment+=new[n]
        else:
            erxperiment+=m
        final="".join(experiment)
    return final
    print final

#6

here:

new=text.split(text)

split(), what you supply the parentheses is the separator character, which should be spaces (which is the default)


#7

Thank you so much :grinning:
Finally solved this problem.


#8