I think I need help


#1



Censor


I think I'm close but i don't know.


I don't know


def censor(text, word):
    a=text.split();
    b = ""
    for i in range(len(a)):
        if (a[i] == word):
            j = a[i].replace(a[i], '*' *len(word));
            b = "".join(j)
        else:
            b ="".join(a[i])
        print b
censor(raw_input("enter a sentance or word you want to be censored"), "hack")


#2

to be honest, not really. here is what i would do:

a=text.split();

remove the semi-colon from this line. This is python, not C or JS

this:

     for i in range(len(a)):
        if (a[i] == word):

is good.

now, if this condition is true, we want to update our list (a) with indexes:

a[i] = # set this list index to asterisks

this will update the list with asterisks (given we have to censor this words)

then after the loop, join the list and return the string

ditch the rest of the code

in short: we split the string into a list, then we loop over the list, replacing all words which requires censoring, then join the list into a string, and return the string


#3

Thank you so much it worked :slight_smile:


#4

do you also understand it?


#5

I think so and the the reason I added the raw_input was because i wanted to make it useable.


#6

this is the code i ended up using

def censor(text, word):
    a=text.split()
    b = ""
    for i in range(len(a)):
        if (a[i] == word):
            a[i]="*"*len(word)
            b = " ".join(a)
    return str(b)
censor(raw_input("enter a sentance or word you want to be censored"), "hack")

#7

the asterisk went in a[i]= "*"len(word)


#8

one small thing, this line:

b = " ".join(a)

i would place it outside the loop. This way, the list into a string at the end. no need to do this each iteration of the loop


#9

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