Minor problem in 10/15


#1

in the code:

def censor(text, cens):
lst = text.split()
result = ""
for word in lst:
    if word != cens:
        result += word
    else:
        word = "*" * len(word)
        result += word
return result

i am given the error:

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

how do i maintain the spaces in the result?


#2

My very similar solution to this, but with a list instead of a string:

def censor(text, word):
    splitted = text.split()
    result = []
    for x in splitted:
        if x == word:
            result.append("*" * len(x))
        else:
            result.append(x)
    final_result = " ".join(result)
    return final_result

(The spaces are added with the " ".join(result) )


#3

@gangstalker below is you code corrected with comments

def censor(text, cens):
    lst = text.split()
    #here instead of using a string and adding to it
    #each time we can just use list
    result = []
    for word in lst:
        if word != cens:
            #if the word is not to be censored
            #we just add it to the list using append()
            result.append( word)
        else:
            #same here we use append to add the censor to the 
            #result list
            word = "*" * len(word)
            result.append(word)
    #here we join the elements in the list by spaces
    #because when we did the text.split() earlier
    #the text was split by spaces(the spaces were not kept)
    # so when we do this join we get the sam string but with censors
    return " ".join(result)

print censor("hey hey hey didi", "hey")

Also going by your original logic you can do this

def censor(text, cens):
    #here you split the text andby default will be split by spaces
    lst = text.split()
    
    #trace statement
    print "The split text is now: %s"%(lst)
    
    #you create an empty string
    result = ""
    
    #you start looping through the split list
    for word in lst:
        #if the current word is not to be censored
        if word != cens:
            #here you have a pitfall
            # when you just add the text to the result like this
            # you do  not preserve the spaces that were originally added
            #to fix that you need to do this
            result += word+" "
        else:
            word = "*" * len(word)
            #Here you have the sam pitfall discussed earlier
            result += word +" "
        
        #trace statement
        print "The current result is now :: '%s'"%(result)
    #here because of the way we have added our words to the result
    #there will always be an extra space at the end of result
    #so we need to strip it by callin rstrip()
    return result.rstrip()

print censor("hey hey hey didi", "hey")

you can test this one here
Click here to labs


#4

thanks for the help you guys :smile:


#5

what is wrong with my code:

def censor(text, word):

strin = text.split()
#strin.split()

print strin

res = []

for i in strin:

   if  i == word:

       wor = "*" * len(i)

       res.append(wor)
       
   else:

       res.append(word)
        
       
return " ".join(res)

it gives me ['hey', 'i', 'am']
am am **
None as output