Censor -- why doesn't this code run?


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/4

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


#2

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


#3

Thank you!

Turns out I over-complicated the code --

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


#4

Yea, i believe that is one of the cleanest solutions


#5

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


#6

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


#7

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.


#8

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:


#9

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


#10

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