What is wrong in here?
What is wrong in here?
where did you define
count? I don’t see it anywhere.
you can’t update a string, strings are immutable.
text is a string. What did you intend to do here?
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
would give me the index of w in the sentence.
Okay, you are certainly moving in the right direction
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
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?
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
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
I agree! I wish I could see the problem
I don’t know
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
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!!!