Censor


#1



what's wrong in my code..??


def censor(text,word):
    a= len(word)
    b=0
    c=''
    for i in range(0,len(text)-len(word)):
    
        while b!=a:
            c+=(text[i+b])
            b=b+1
            if id(c)==id(word):
                for j in range(0,a):
                    text[i+j]+='*'
    return text


it gives the string unchanged.....


#2

uhm... i don't understand how this code is going to help you achieve your goal.

for example, about the id() function you use:
Return the “identity” of an object. This is an integer (or long integer) which
is guaranteed to be unique

guaranteed to be unique, how can they ever be equal then?

i don't understand why you why you use this loop:

for i in range(0,len(text)-len(word)):

i know gives you indexes of letters (but not all, given you subtract word length)

it doesn't make much sense, because then you have a while loop to loop over the text again?

I recommend you to start again, start by using split() so you have a list with the words from text, that would be a good start, then you can loop over this list and compare each word of text with the word that require censoring


#3

so even if the characters or strings in both variables are same, the id() will not be equal....if not then how can we compare two strings in python?
for i in range(0,len(text)-len(word)):
in program i used a for loop:

for j in range(0,a):
text[i+j]+='*'
When the i will be on it's last index which is: len(text)-len(word)-1 it will check if the word exist ahead in the string text, after that index the occurence of word is not possible because no. of characters are lesser than that in string word.


#4

comparing strings is easy?

c = "test"
d = "hello"
if c == d:
  print True

you can just compare variable which hold strings.

right, i personally just recommend you to start again Your method is not going to work.

take this piece of advise:

this will get you to the solution


#5

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