Censor 10/15 - code returns " " instead of " * "


#1

On exercise 10/15, the Censor program, I’m having some trouble. The error is
Your function fails on censor("hey hey hey","hey"). It returns " " when it should return "** ".
So question is, why is my code not replacing the censored word with *s? Also, why is it returning blank spaces?

def censor(text, word):
	newword = ""
	newtext = text.split()
	for item in newtext:
		if item == word:
			newword += ("*" * len(item))
		else:
			newword += " ".join(item)
	return " ".join(newword)

#2

After some additional tweaking, I’ve gotten a code that actually seems to work properly and satisfies all the requirements of the lesson. However, I still receive the same error from Codeacademy. This is my updated code:

def censor(text, word):
	newword = ""
	newtext = text.split()
	for item in newtext:
		if item == word:
			newword += ("*" * len(item) + " ")
		else:
			newword += item + " "
	return newword

On testing, the code works properly. The phrase calling censor on "hey hey hey", "hey" returns "*** *** *** ". The only thing I can think that’s causing it to fail is I have a space after every instance, including the last word. I’m guessing that’s what Codeacademy isn’t liking. How would I fix that?


#3

Your testing method is wrong (show/tell me)
And, since the error message codecademy gives you tells you what’s different, that should make it easy to spot whether you’re considering that in your test

You’ve probably got some confirmation bias in your test, seeing what you expect but not actually testing whether it’s the exact same result. (Or you simply consider the difference acceptable)


#4

Fair points. What’s tripping me up is I don’t understand the error it’s giving me. It says it’s returning a simple " ", and it should be returning "** ". Shouldn’t it be returning “*** *** ***”? Or if it’s only checking to see if one of the words has been censored, shouldn’t it still return a full three *s? Why is the error only looking for two *s and a space afterward? That’s really tripping me up.


#5

If it says exactly what displays in your post (ie not a formatting error on your part)

Then yeah. Also that’d be a recently-ish introduced bug. (I have no influence whatsoever on that)

It’s the trailing space. My assumption was that you’d be receiving the correct and incorrect values, making for easy comparison, but I suppose someone botched that.


#6

I think they might have applied markdown formatting to the error message, causing asterisks to be interpreted as cursive/bold formatting

Really the error message for your first piece of code was:

Your function fails on censor("hey hey hey","hey").
It returns "* * * * * * * * *" when it should return "*** *** ***".

#7

Finally figured it out! It turns out you were right on the error message being formatted (I tried rewriting the code several different times, and noticed the error message was occasionally bold as if certain words were surrounded by *s). What I finally figured out, is the += command wasn’t ideal here. This is my working code:

def censor(text, word):
	newtext = text.split(" ")
	new_sent = []
	for item in newtext:
		if word != item:
			new_sent.append(item)
		else:
			new_sent.append("*" * len(word))
	return " ".join(new_sent)

Any thoughts or suggestions for improvements? Thanks for your help, I really appreciate it.


#8

Some comments, yes, but none of which I’d suggest you act on

Entirely cosmetic, 4 spaces are generally preferred over tabs for python. But codecademy’s editor isn’t entirely well behaved so if tabs are working for you just keep doing that.

I’d switch places on the if/else parts just to turn != into ==. “not” is more difficult to think about. Again, entirely cosmetic.

And then I’d not make the function at all, because str’s replace method already does all of this. However, for the sake of the exercise one has to draw the line somewhere…

str’s split already splits on whitespace. Splitting on space is more specific, I’d still leave it to default…It already does the right thing

+= with strings would be fine for spaces, it just takes a different action at either the first or last iteration. Storing the words in a list is still better though, because strings don’t support appending - list is the right data structure to use. (And it obviously avoids the special case for the first or last iteration)