Censor


#1

Please tell me what's wrong with my code.

def censor(text,word):
    l = []
    n = text.split()
    for x in n:
        if x != word:
            l.append(x)
    else:
        l.append('*'*len(word))    
    return ' '.join(l)

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


#2

Hello,

else: is in wrong place -> in your code it works as for - else, not if - else.
Just add tabs in lines: else and l.appand() and code works well.


#3

Thanks a ton! I had a hard time with this. :slight_smile:


#4

Just finished ranitng about the use of l as a variable name and come crashing into the same scenario. We might instead of just calling something a list with the symbol l try calling it what it contains. Then we give it meaning. If the list contains interim results, then call it that...

result = []

Now consider where the else is placed in your code. It looks to be associated with the for, not the if. That is the real problem, regardless what you call your object.


#5

A post was split to a new topic: Censor


#6

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