Censor pls help


#1

def censor(text, word):
    new = text.split()
    stars = "*" * len(word)
    for i in range(len(word)):
        if i == word:
            new[i] = stars
    return " ".join(new)
print censor("some people are stupid very mutch stupid", "stupid")

need some help, i think i am close!


#2

you are really close, look:

def censor(text, word):
    new = text.split()
    stars = "*" * len(word)
    for i in range(len(word)):
        print i
        if i == word:
            new[i] = stars
    return " ".join(new)
print censor("some people are stupid very mutch stupid", "stupid")

i added a print statement for you, as you can see i are the indexes, so if i == word can't be true, use the index (i) to get the word from text


#3

I'm trying to understand your statement.

I made if I != word but it put the "*"'s in the wrong spots.
am i thinking in the right direction?


#4

my explanation kind of failed, i should write a better one. Okay, here:

for i in range(len(word)):
    if i == word:

what is i? i is a integer. Because you use range(), you get integers which you can use as index values to get each word in text

but at your if condition you are comparing i (integer) with word, you should compare text[i] with word


#5

Not at all, doesn't mean that if I don't understand that your explanation was insufficient, simply means we think in different ways.:slight_smile:

So i changed my function like you suggested comparing text[i] with word but in the console it prints out the words without the *'s. I also changed new[i] = stars to new[i] == stars but still no luck.


#6

can i see an updated version of your code? (full code)


#7

sure

def censor(text, word):
    new = text.split()
    stars = "*" * len(word)
    for i in range(len(word)):
        if text[i] == word:
            new[i] == stars
    return " ".join(new)
print censor("some people are stupid very mutch stupid", "stupid")

#8

this:

new[i] = stars

was right, you want to update the item in the list to starts since you want to censor the word.

I am not paying attention and i am being sloppy.

this line:

if text[i] == word:

text is a string, so this will give us letter. We need new, this list which contains our sentence. then we can do new[i] to get the word from the list


#9

why doesn't it * the last "stupid"? Am I missing someting?


#10

you loop over word, which contains the word you want to censor

You want to loop over new since that is the list which contains all the words which needs checking if they need to be censored


#11

THANK YOU! It's working!!:grinning:


#12