Censor


#1

def censor(text,word)
z=len(word)
print(z)
z=''z
print (z)
t=text.split(" ")
k=[]
for i in t:
if (i==word):
print ('Its a match')
i=i.replace(i,z)
k+=i

else:
k+=i

print (k)
k=' '.join(k)
print(k)
print censor('Hi how are you','are')
HI..
I am getting a little error in the splitting of words as a final output..


#2

please post your code correctly using markup:

so we can run your code


#3

def censor(text,word):
    z=len(word)
    print(z)
    z='*'*z
    print (z)
    t=text.split(" ")
    k=[]
    for i in t:
        if (i==word):
            print ('Its a match')
            i=i.replace(i,z)
            k+=i

        else:
            k+=i

            print (k)
            k=' '.join(k)
            print(k)
print censor('Hi how are you','are')

HI..
I am getting a little error in the splitting of words as a final output..


#4

where did the indent of your code go? I can't run your code like this


#5

updated the code in earlier post.


#6

using .replace() allows for a single line solution, replace() can find all instances of word and replace them. its dull

instead, .append() ztok list if i == word, else append i to k list

this way, we use if/else to determine if word needs censoring, creating a new list with the censored sentence

then after the loop join the list into a string, and return the censored string


#7

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