Censor : helped needed (almost there)


#1



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


this is what it printed in console:
['hey', 'hey1', 'hey2']
hey
hey
empty_str:***

hey1
hey
empty_str:******

hey2
hey
empty_str:*********

None

my error message is written below:
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "* * * * * * * * *" when it should return "*** *** ***".


I want to replace hey to ***
so i want it to return
censor("hey hey1 hey2", "hey")
-> *** ***1 ***2
but it's printing only * without spacing


def censor(text, word):
    empty_str = ""
    
    # split the words
    new_text = text.split(" ")
    print new_text
    
    #check if word is in the text
    for i in new_text:
        print i
        print word
        # replace with *
        if word in i:
            replaced = (len(word) * "*")
            empty_str += replaced
            print "empty_str:" + empty_str + "\n"
    
    return " ".join(empty_str)

censor("hey hey1 hey2", "hey")


#2

def censor(text, word):
    empty_str = ""
    new_text = text.split(" ")
    for i in new_text:
        if word in i:
            replaced = (len(word) * "*")
            empty_str += replaced
    return " ".join(empty_str)

There are few things to note..

1.new_text = text.split(" ")
It means you breaking your text at " ".

2.empty_str += replaced , As you've eliminated spaces from your text,
You need to add one space to your string.

3.You're censoring the words but you also have to add all words apart from censored words in your string.
Introduce an else statement and add them ,do not forget to introduce (add) a space there.

4.return " ".join(empty_str) ,.Join() method is not needed here as your output is string only and that will create an extra space between different characters of string.

while you return your final string,It will have final space after it (due to step 3)
You can get rid of it using..

String slicing or
use .rstrip() method


#3

first of all, thank you for your kind reply.
It helped me understand the concept and my situation.

Here is what I've done to solve my problem.
1. I don' think there is a problem using split(" '"). After running the testcases, it doesn't show problem.
2. since I was having hard time manipulating string, I changed the format with list (After doing some research).
Then used ".append()" method to add all the words by sequence.
3. yes I totally forgot to handle non-censored part. I used else statement to also append in the new empty string.
4. without this " ".join() method, mine would print in list format. ("", "", "***") so i wanted to add " " between *** *** ***.

The following code is my final source code that is successfully passing:
"""
def censor(text, word):
empty_str = []
new_text = text.split(" ") # split the words
replaced = len(word) * "*"

#check if word is in the text
for i in new_text:        
    # replace with *
    if word in i:
        empty_str.append(replaced)
    else:
        empty_str.append(i)
return " ".join(empty_str)

"""

Thank you once again for your help!


#4

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