Simple code not working


#1


Can someone please tell me why my replace method isn't working here.


def censor(text, word):
    text_brk=text.split()
    #print text_brk
    for i in range(0, len(text_brk)):
        if text_brk[i]==word:
            text_brk[i] = [text_brk[i].replace(text_brk[i][i], 'x') for i in range(0, len(text_brk[i]))]
            
    print text_brk

censor('this is a nice day','day')


#2

Another topic? It is a forum, not a chat where instant reply is required.

Well, i wouldn't call this line:

text_brk[i] = [text_brk[i].replace(text_brk[i][i], 'x') for i in range(0, len(text_brk[i]))]

a simple solution, i see a few problems: You want to replace the word (if found) with an list? Why? I would replace it with a string, not an array. then there is a problem here:

text_brk[i][i]

you don't have a multi-array index, yea you are creating one now, but you don't have it yet


#3

How would you suggest I go about replacing the characters in the word with a string of (*) of equivalent length then? I was assuming that my loop would simply print the elements as a string. I'm not familiar with arrays.


#4

simple replace them with * times the length of the word?


#6

Hey, I tried that here, but I'm getting a syntax error, any idea why?

here's the code...

def censor(text, word):

    text_brk=text.split()
    #print text_brk
    for i in range(0, len(text_brk)):
        if text_brk[i]==word:
            text_brk[i]='*'*(len(text_brk[i])
    print text_brk

    
            
    
    

    
    
censor('this is a nice day','day')

#7

make sure all brackets match in this line:

text_brk[i]='*'*(len(text_brk[i])

two opening brackets versus one closing brack isn't going to work


#8

Oh ■■■■ that was one stupid mistake! thanks so much !