10/15 help


#1

hi
I am stuck on number 10 and have been for a few days now. I have gone through many versions of the code but none seem to work. please tell me the answer possibly an explanation too to put me out of my missilery.

Thank yo:smile: :smile:


#2

str.split( ) will split your text into a list of individual words... Give this new list a name...
Then iterate over this new list testing FOR each ITEM IN the LIST == word
Use an IF loop to replace the offensive word with ""(len(word))...
Then rejoin your list up into a phrase using " ".join(list)

Hope this helps...


#3

This is my solution: hope it could give you different idea :smile:

def censor(text,word):
a = ""
for i in range(len(word)):
    a = a + "*"
if word in text:
    text = text.replace(word,a)    
return text

#4

thank you

def censor(text,word):
a = '*' * len(word)
if word in text:
text = text.replace(word,a)
return text
print censor('this hack is wack hack','hack')def censor(text,word):
a = '*' * len(word)
if word in text:
text = text.replace(word,a)
return text
print censor('this hack is wack hack','hack')


#5

What's wrong with my code:

def censor(text, word):
array = text.split(" ")
for x in array:
    if word == array[x]:
        array[x] = "*" * len(word)
return " ".join(array)

#6

This worked for me, and it's close to your code:

def censor(text, word):
    censored = []
    for i in (text.split()):
        if i == word:
            censored.append(("*" * len(word)))
        else:
            censored.append(i)
    return " ".join(censored)

#7

import re
y = " "
text = raw_input("any old string of text: ")
word = raw_input("the word you wish to replace with asterisks: ")
def censor(word,text):
y = " ".join("*" * len(word))
lst = re.sub(word,y,text)
return lst

print censor(word, text)

Comment: Still in the works. My problem so far is that it goes after all the enumerated chars and then ignores the spaces.


#8

this is my simple approach

def censor(text,word):
if word in text:
newtext = text.replace(word,"*" * len(word))
return newtext


#9

def censor(word,text):
return text.replace(word,"*" * len(word))

print censor("hey", "hey hey hey")

//*works, but throws an error for an as yet unknown reason.*//


#10

Thanks for the hint on cleaning up my code! It works but the autograder still says I am in error.


#11
def censor(text,word):
                  text = text.replace(word,"*" * len(word))
    return text