Censor-solved


#1

def censor(text, word):
    count = len(word)
    tear = text.split()
    new = ""
    for item in tear:
        if item == word:
            new = new +" " + ("*" * count)
        else:
            new = new + " " + item
    sliced = new[1:]
    return sliced

Why this is not working phython (practice make perfect) censor
What is function of this line?
#2

Do you have a question?


#3

i'm sorry, thought it might help someone


#4

Well just for future reference, It is best not to just post answers into the forums because that encourages people to not learn the code but just pass the lessons. But I can understand wanting to help people so if someone asks in the forum for this lesson you can give them hints or basic outlines to this lesson to help them get the answer! :smiley:


#5

We try to nudge people in the right direction, not just give them solutions. That way people really learn code from the site instead of being able to look it up.


#6

understood, thanks. i will adhere to that


#7

understood. i wont post solutions again


#8

Yeah sorry about that. We both posted at the same time with the same message lol xD


#9

def censor(text, word):
new_word = ""
new_list = ""
for i in word:
word = word.split()
new_word = word.replace(i,"*")
if word in text:
new_text = text.replace(word,new_word)
return new_text

i didnt understand the error , it says -
Oops, try again. Your code looks a bit off--it threw a "'list' object has no attribute 'split'" error. Check the Hint if you need help!


#10

here is what the question says:
Write a function called censor that takes two strings, text and word, as
input. It should return the text with the word you chose replaced with
asterisks.
firts of all you are supposed to replace word with asteriks if it is found
in text.. but in your code you looped through word instead of text., the
change change to asteriks should take place in text so we are supposed to
check for "word" in text, which therefore means that we should loop through
"text" and serach for "word".
the split() function split the whatever is in your text text into and
create a list of each word in it..
instead of splitting word you should have splitted text like this:

text_split =  text.split()

after which you will loop through each the new text_split text like this:

 for item in text_split:

then you will check if that word you want to replace is the current item in
list, so your code will go like this

count = len(word)
new = ""
text_split =  text.split()
for item in text_split:
    if item == word:
         new = new +" " + ("*" * count)
    else:
           new = new + " " + item
    sliced = new[1:]
    return sliced

the new with the empty string in the code will contain your new asteriks if
it is found in text
the len() function counts the number of letters in your word to determine
the number of asteriks to replace it with
("*" * count)- this produce the equivalent number of letters counted in
your word as asteriks
sliced= new[1:] - removes the empty string at the beginning of your "new"
variable
your error is in splitting word instead of text

your code formatted this way will work:

def censor(text, word):
    count = len(word)
    text = text.split()
    new_string = "" #new_list changed to new_String
    for i in text:
        if i == word:
            new_string = new_string + " " + ("*" * count)
        else:
            new_string = new_string + " " + i
    sliced_string = new_string[1:] # removes the empty string at the
beginning
    return sliced_string

read through the code and try to understand the logic.the main point there
is the logic not the syntax. just the logic, once you get the logic the
syntax becomes simple


#11

splitting isn't necessary. you just need to replace the word within the text string with the correct number of "*". The len function should help to get the number.


#12

Hi, I cannot get one thing about this code.
When building the new_string, why do you need to add " "?


#13

To put a space between words after we've taken it apart.


#14

Also, if anyone has any more questions about this lesson, please make a separate forum post! It is easier to get help and more organized than several people posting on an old post.


#15

thanks a lot , you are of great help .


#16

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