BUG in Python Practice Makes Perfect, Lesson 10: Censor


#1

BUG in Python Practice Makes Perfect, Lesson 10 - Censor:

The directions read:

"Write a function called censor that takes two strings, text and word, as input. It should return the text with the word you chose replaced with asterisks."

def censor(text, word):
    text = text.replace(word, "****")
    return text
    
text ="Oh shit"
word = "shit"
print censor(text, word)

The above function and call prints "Oh ****" to the console, but Codecademy is throwing an error stating:
"Oops, try again. Your function fails on censor('hey hey hey','hey'). It returns '* **** ****' when it should return ' *** ***'."

This dialog doesn't make sense either. It states its returning what it should return (i.e., "It returns '* **** ****' when it should return ' *** ***'.")


#2

well, the exercise runs multiply test cases on your function, and hey is 3 letters, and not 4. So you might want to replace word with * times the length of word


#3

Actually, I tried that. I'm not sure what I'm doing wrong, but I keep getting the compiler error "expected a character buffer object" with len(word). This is what I've tried:

def censor(text, word):
newChar = len(word)
text = text.replace(word, ["[asterisk goes here]"]*newChar)
return text

And:

def censor(text, word):
text = text.replace(word, ["[asterisk goes here]"]*len(word))
return text

I don't get why this doesn't work..?


#4

Thanks! I just figured it out. I just needed to remove the [], because word is a String, not a List. Sorry, duh.

Although, my simple dot replace from the first try works.