Censor -- why doesn't this code run?



I get this error: an integer is required
I was trying to simplify the code and not use the split() or join() functions. Not sure why I get that error.

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


replace has 3 arguments, why? It should be two. You do text.replace, so why specify text again inside the brackets? You can read about replace here.

Now, that solves the error, doesn't mean your code works now. But this should help you


Thank you!

Turns out I over-complicated the code --

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


Yea, i believe that is one of the cleanest solutions


It is great practice to attempt this problem without using any built-ins. We learn more that way.


True, which means we can use: for, if, return, print? Or did i forget something now


No, you did not forget anything. I only commented how we can make things interesting by practicing bare bones algorithms which we can later refactor with built-ins. Nothing wrong with your comment. Sorry if it sounded that way.


I was just curious what we where allowed to use, if we add to replace the for loops with goto, it would become messy (does python even have goto?)

Anyway, sounds like a good challenge :slightly_smiling:


The instructions don't place any restrictions on methodology and anything that works will pass, I suspect.


Not that I am aware, unless it supports labels. Functions and loop constructs pretty much fill the requirements.