Censor- Practice Makes Perfect


#1




def censor(text, word):
array = text.split()
tot_text = ''
for each in array:
if each == word:
tot_text += '*' * len(each)
tot_text += ' '
else:
tot_text += each
return tot_text

This is my code. I can't find any errors and it returns exactly what I want. However, I can't move on to the next exercise because "Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***"." keeps popping up. What do I do?


Replace this line with your code.


#2

lets replace the spaces with underscores (so we can better see where the spaces are), you have:

***_***_***_

you should have:

***_***_***

the easiest solution would be simply to slice it off? Slicing has been covered at this point, you also use string slicing in pyglatin


#3

Ohhh. Ok. So I was pretty confused about the slicing off bit so this is what I did. It seems to be working now. Thank you.

def censor(text, word):
    array = text.split()
    tot_text = ''
    counter=0
    for each in array:
        counter+=1
        if each == word:
            tot_text += '*' * len(each)
            if counter != len(array):    
                tot_text += ' '
        else:
            tot_text += each
            if counter != len(array):    
                tot_text += ' '
    return tot_text

Also, thank you for ignoring the "Replace this line with your text". This was my first time posting an ask so I was a bit confused to be honest.


#4

but you have done list slicing before:

print "hello world"[-5:]

you could have used it here as well.


#5

Oh right! Sorry. Thank you for the help


#6