Issues on 'Censor' - exercise 10 - 10


#1

Hi all,

Am getting this error on exercise 10 - 10:
Your function fails on censor(“hey hey hey”,“hey”). It returns " " when it should return " ".

This is my code:
def censor(text,word):
newword = “”
newsentence = “”
splitlist = text.split()
for w in splitlist:
if w == word:
for i in range(0, len(w)):
newword += “*”
newsentence += newword + " "
newword = “”
else:
newsentence += w + " "
return newsentence

It’s certainly not the same code as the solution - but it seems to get the job done (if I print instead of return it presents the correct string. What am I getting wrong?


#2

Here is the code with correct formatting:

image


#3

By inserting space characters we can mess up the final outcome. That may not be the only problem, but it will be one of them.

Couple of things to look into are the string repeat operator (*) and the .join() method.

An example of the repeat operator…

>>> def censor(text, word):
	for term in text.split():
		if term == word:
			print ('*' * len(word))
		else:
			print (word)

			
>>> censor('hey hey hey', 'hey')
***
***
***
>>> 

#4

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