Censor


#1



Censor

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

Not sure why my output keeps the [] or have ' ' around the ***.

def censor(text,word):
    results=[]
    for words in text.split():
        if words==word:
            results.append('*' * len(words))
            
        else :
            results.append(words)
            
    return results


#2

here:

return results

results is a list, you need to return a string. Maybe you can use .join()?


#3

def censor(text,word):
results=""
for words in text.split():
if words==word:
results.join('*' * len(words))

    else :
        results.join(words)

return results

here is my error now...

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


#4

i said you might want to use join here:

return results

to get from list to string, i didn't say you should replace append with join, did i?


#5

def censor(text,word):
result=[]
results=""
for words in text.split():
if words==word:
result.append('*' * len(words))

    else :
        result.append(words)

return results.join(result)

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

How do I get spaces in my code with a join?


#6

here:

return results.join(result)

results should be a space, so the words are joined together with spaces


#7

You can put a space between the quotes before .join()
This joins the elements of your list, but inserts a space between each one.

return " ".join(result)


#8

I get an error when I try to put " " in front of my join.


#9

please post the code


#10

I figured it out.

def censor(text,word):
    result=[]
    results=" "
    for words in text.split():
        if words==word:
            result.append('*' * len(words))
            
        else :
            result.append(words)
            
    return results.join(result)

#11

the code is fine, try refreshing page/different browser


#12

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