10. censor


#1


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


Oops, try again. Does your censor function take exactly two arguments, a text string and a word to replace with asterisks? Your code threw a "censor() takes exactly 1 argument (2 given)" error.


def censor(text, word):
    split = text.split(" ")
    new_text: ""
    for i in split:
        if i == word:
            i = "*" * len(word)
        new_text += i + " "
        
    return new_text


#2

Try my code. It is much easier to read, it only takes 2 lines! (Besides the print statement)

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

print censor('hi bob', 'bob')

You put a space in between the return statement. Delete that.


#3

Two flaws in your code:
1. Line 3: colon should be substituted by equals sign
2. add one Line after the for loop to eliminate the last space in new_text

You full code can be:
def censor(text, word):
split = text.split(" ")
new_text = ""
for i in split:
if i == word:
i = "*" * len(word)
new_text += i + " "
new_text = new_text[:-1]
return new_text


#4

The code you used worked for me, but can you explain why it works because I'm confused how it works.


#5

Okay! :exclamation:

1) First we define a function called censor with 2 arguments called text and word.

2) In the function we use the function text.replace and replace word for * for the length of word. (Yeah sounds complicated, but I bet you probably know what I am saying here.)

3) Return that (Step 2)

4) Outside the function we call the function with the text, Hi Bob, and with word, Bob.

That should print as Hi ***

Hope this explanation helps. :wink:

~Candy :slight_smile:


#6

def censor(text, word):
cens = ""
new_text = ""
temp = ""
i = 1

for letter in word:
    cens += "*"

for letter in text:
    if letter == " ":
        new_text += temp
        temp = ""
        new_text += letter
    else:
        temp += letter
        if temp == word:
            new_text += cens
            temp = ""
        elif i == len(text):
            new_text += temp
    i += 1

return new_text

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

if you just start learning Python and do not know all this "replace" and "split" functions :wink:


#7

I just give another way to reach the goal,which does't use .split() or .join()
here's my code:

def censor(text,word):
    stars = '*'*len(word)
    string = ''
    txt = ''
    i = 0
    while i < len(text):
        string = text[i:i+len(word)]
        if string == word:
            txt += stars
            i += len(word)
        else:
            txt += text[i]
            i += 1   
    return txt
    
print censor(raw_input("Your text:"),raw_input("Your censor word:"))

#8

@candycornlover1 Thank you for explaining that helps so much more now! :grinning:


#9

Hey no problem! :slight_smile:


#10

That was a lot easier to understand


#11

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