Censor666


#1

Hi guys, i got stuck on this part, can you please explain what am i doing wrong and give useful hint?

Oops, try again. Your function fails on censor("Yo go fro yo go","go"). It returns "Y* * fr y* **" when it should return "Yo ** fro yo **".

def censor(text, word):
    new = ""
    for i in text:
        if i in word:
            i = "*"
        new += i
    print(new)
    return new

#2

here:

if i in word:

why use in? Its possible, but checking if equal would be more efficient.

and finally, here:

i = "*"

we need as many asterisks as the word is long


#3

Now it shows nothing))))

def censor(text, word):
    new = ""
    leng = len(word)
    astt = "*" * leng

    for i in text:
        if i == word:
            i = astt
            new += i

    print(new)
    return new

#4

why did you change the indent of new += i? Now i only gets added when censoring is applied, i should be added in both cases (which you had, which was good)

another problem, how are you going to preserve the spaces in the sentence?


#5

now i have correct indent, but nothing changed


#6

that explain why you used in, i will contain each letter of text. Which won't work, because certain letters of words which don't need censoring do get censored.

check the hint, use .split() to get split the text string into a list so we can loop over each word in text rather then each letter


#7

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

i cant make .join() function work properly.

def censor(text, word):
    new = ""
    leng = len(word)
    astt = "*" * leng
    items = text.split()

    for i in items:
        if i == word:
            i = astt
        new += i
        new += " "

    print(new)
    return new

#8

the .join() function only works when new would have been a list (join is a function, which, well joins a list into a string)

the problem with your current solution is that you have an additional space at the end, you could string slicing to get rid of it.

You should go for new being a list, lists are mutable in python while strings are not, so lists would be a more efficient solution.


#9

i like.. cant find the way out with this fkn join func, could you please write solution?


#10

that would be a violation of the guidelines.

Why not? You are so close, you just need to slice of the trailing space you have the end of your sentence

if you go for join, did you ensure the use of lists?


#11

i think i just tried all alternatives i have


#12

show me what you tried


#13

def censor(text, word):
    new = ""
    leng = len(word)
    astt = "*" * leng
    items = text.split()

    for i in items:
        if i == word:
            i = astt
            i = " ".join(i)
        new += i
        

    print(new)
    return new

def censor(text, word):
    new = ""
    leng = len(word)
    astt = "*" * leng
    items = text.split()
    items = " ".join(items)
    for i in items:
        if i == word:
            i = astt
        new += i

def censor(text, word):
    new = ""
    leng = len(word)
    astt = "*" * leng
    items = text.split()
    
    for i in items:
        i = " ".join(i)
        if i == word:
            i = astt
        new += i

def censor(text, word):
    new = ""
    leng = len(word)
    astt = "*" * leng
    items = text.split()
    
    for i in items:
        if i == word:
            i = astt
            i = " ".join(i)
        new += i

def censor(text, word):
    new = ""
    leng = len(word)
    astt = "*" * leng
    items = text.split()
    
    for i in items:
        if i == word:
            i = astt
        new += i
        
    new = " ".join(new)

def censor(text, word):
    new = ""
    leng = len(word)
    astt = "*" * leng
    items = text.split()
    
    for i in items:
        
        if i == word:
            i = " ".join(i)
            i = astt
        new += i

..i tried everything i had in my head


#14

using .join() would involve new being a list, you would need to .append(i) to the list rather then extending the list (+=), then after the loop join the list into a string and return the string


#15

oh my gush, thank you alot
didnt know at all, that i have to append all in list
and sorry for being dumb...

def censor(text, word):
    new = []
    leng = len(word)
    astt = "*" * leng
    items = text.split()
    
    for i in items:
        if i == word:
            i = " ".join(i)
            i = astt
        new.append(i)
        
    new = " ".join(new)
        
    print(new)
    return str(new)

#16

This exercise are tricky, but you solved it in the end :slight_smile: That is the most important bit, you solved it, i only helped


#18