Please tell me what's wrong with my code.

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

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



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.


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


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.


A post was split to a new topic: Censor


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