How can i use split() to make censor?

What is wrong in here?
image

where did you define count? I don’t see it anywhere.

Here:

text[w[count]] =

you can’t update a string, strings are immutable. text is a string. What did you intend to do here?

1 Like

I want to change the w (the word I want to remove) to asterisks.
I can change a list by the index inside it, so I thought

[w[count]]

would give me the index of w in the sentence.

image
help?

Okay, you are certainly moving in the right direction :slight_smile:

however, w[count] won’t magically give you the index you are after.

there are a couple of ways we can achieve this:

create a variable named index, set it to zero and increase index every iteration of the loop
we could use range() in the loop
python also has a built-in enumerate function which would be a good fit here

1 Like

Okay
image
and now?

Now comes the point that working from screenshots becomes really difficult. Please copy paste your code to the forum

Seems you glue/join your list back together in such a way that the spaces between the words are missing.

Have you tried calling your function to see what output you are getting?

def censor (text, word):
  length = len(word)
  words = text.split()
  count = 0 
  for w in words:
    if w == word:
       words[count] == "*" * length
    count += 1 
  sen = "".join (words)
  return text

I called the function now and it gave me the original sentence…,

That should be helpful, right? Returning the original text can’t be the intention. What should we return and which variable holds this value?

:sweat_smile:
I changed the return to return the sen and added a variable named “stars” that equals "*" * length before the For sentence.
It is still doesn’t working :frowning:

And why doesn’t it work? What output doe you get and is this what you expected?

I can help you, but in the long run, you also need to learn to debug. The question(s) I ask above are questions you need to learn to ask yourself :slight_smile:

1 Like

I agree! I wish I could see the problem
I don’t know :frowning:
clue ?

Based on the changes you say you made (which I would rather see, in code), I guess your sentence has no spaces between the words

I changed that as well!

def censor (text, word):
  length = len(word)
  words = text.split()
  count = 0
  stars = "*" * length
  for w in words:
     if w == word:
        words[count] == stars
     count += 1
  sen = " ".join (words)
  return sen
  print sen

here:

words[count] == stars

you check if these values are equal to each other, why? You should (like you said) update the word in the list with asterisks (using assignment)

Oh I’v missed it, thanks!!!