Can't get the error in code:


#1


#2

There is a space at the end of your output that is throwing it off.

"*** *** *** "

VS

"*** *** ***"

#3

But Still i am not able to find out a solution to remove the spaces from last..
My code is

def censor(text,word):
i=[];
text=' '+text+' '
s=""
ctr=0
for a in text:
if a!=' ':
ctr+=1
s=s+a
if ctr==len(text):
i.append(s)
else:
i.append(s)
s=''
for b in range(len(i)):
if i[b]==word:
i[b]=""len(word)
n=''
for c in i:
n=n+c+' '

return n

#4

To start with you should learn how to codify your code so it displays corrctly. Use of the </> key will display your code correctly or you can use mark down as follows.

```python

# Code Here

```# End

Next, adding stuff to your code that does nothing to add to the whole makes it complicated and hard to understand.

For this section all you need to do is replace the word of choice with *s so let's make a list of things that have to happen in order to do that.

  • Break the string up into smaller sections, this is because strings are immutable in python. We will use a list for this, we will also split the word at the spaces.

  • Create a function to match the words regardless of capitalization and then switch out with desired string of *s

  • Join the list of strings back together and return it as a whole

Ok, now that we have our steps let's get to putting it together.

def censor(text, c_word):
    hold = text.split(' ') # This built-in method allows us to split the string at the given input
    for index, word in enumerate(hold): # Here we loop through the list of words and use enumerate to get teh index at the same time.
        if c_word.lower() == word.lower(): # Checks to see if the word is the string
            hold[index] = '*' * len(word) # Here we replace the word
    return ' '.join(hold) # This returns the list as a string with a ` ` between each item

That is what you need to do in order to get everything done, as you can see we did not add anything unnecessary so are code looks clean and succinct.

Now that we have a base code to work with let's shorten it,

def censor(text, c_word):
    return ' '.join(((('*' * len(word)) if word.lower() == c_word.lower() else word) for word in text.split(' ')))

See, now that is a nifty one liner that uses super-fast built-in C code. Generators convert your code into bytecode which runs super fast.

# This is the generator
((('*' * len(word)) if word.lower() == c_word.lower() else word) for word in text.split(' '))

I also used ternary functions to change the word if it matches

# This is the ternary function
(('*' * len(word)) if word.lower() == c_word.lower() else word)

The rest is the same as the previous function, using join and split in the appropriate places.

There are also other things you can do like,

def censor(text, c_word):
    return text.replace(c_word, ('*' * len(c_word)))

The built-in string method replace does what our function does but is built-in to work on strings. As the point of the lesson was to create your own function to do this it would not have helped you before to know this.

Moving forward, when you are trying to solve a problem programmatically create a list of steps that have to be completed in order to preform the tasks needed. This will give you the base needed to actually write the code.

As always best of luck!