Censor


#1

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096

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

It shouldn't do this since giving the string "hey hey hey" as an input returns the correct answer *** *** ***

def censor(text, word):
        text = text.split()
        for i in text:
            if i == word:
                i = "*" * len(word)
                text[text.index(word)] = i
        text = " ".join(text)
        print (text)
    


censor("what the hell", "hell")

Thanks


#2

Printing and returning are different concepts


#3

Sorry, my bad... didn't even think about that


#4

I understand all of @rubysurfer45482 's code here, except the following line:

text[text.index(word)] = i

What does this line mean? I think I know what it is doing, but I don't know why.


#5

I used that bit since you can't index a list by a string. text.index(word) gives you back the integer representing the index of the word you're checking and hence the one you want to substitute in the list.


#6

A simple solution

def censor(text, word):
char='*' * len(word)
return text.replace(word,char)


#7

Why does this NOT work??

def censor(text, word):
star = "*" * len(word)
str_list = string.split(text, word)
new_text = star.join(str_list)
return new_text

print censor("this hack is wack hack", "hack")


#8

I cant get that to work for me


#9

Make sure you use proper indentation in your function.


#11

can you please explain what these line are doing
text[text.index(word)] = i
text = " ".join(text)


#12

This line raplaces (word) with i in a list

"text = " ".join(text)" - this line will noy work
it should look like "return (" ".join(text))"


#13

thank a lot for helping me


#14

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