Censor Dispenser Project

Hello everyone,

i’m stuck at Censor Dispenser Propject’s fourth task.
I can’t replace negative words with ‘xxxxx’

This is my code for now.
I can find texts according to terms but i cant really replace negative words right now.

Can you explain what issue you have? There’s more than one way to approach this problem but knowing where you’re at would be helpful. That is, what did you expect to happen (explain your thinking) and what happened instead?

Whilst working towards a solution it may be worthwhile solving this one for a single ‘negative term’ before trying to eliminate all possible words. It’s also worth a quick browse of the following FAQ on submitting code to the forums.

The issue is, i have to change negative words to “xxxx” (x for each character) if negative words in the negative words list, is written 2 times. However, I can’t change negative words into “x” format. I tried string method .replace() but I was trying to use it with argument non string, but it gives me an error, which says I have to use string arguments.

OK, what is your current thought process for the code you posted above? At what point does it stop doing what you want it to do?

I’m not sure what you passed to the replace method but you can find the basic details in the docs- str.replace. Here’s a quick example of it below-

my_str = 'this string is my string'
replace_my_str = my_str.replace('string', 'replacement')
Out: 'this replacement is my replacement'

If you wanted a replacement of a specific length you’d need to find the length you wanted and then create a string with that many characters to pass to it.

Sorry i could not respond you because i realised that i have understood the tasks tottaly wrong and i restarted from the beginning. But now i have another problem.

This is my code.

This is my result and i can’t understand what i’m doing wrong. i want to replace the given word with ‘#’ and i can success it but the main problem is i can’t move the loop forward.

Here in the for loop at the very bottom of the code block, i suppose the code ‘i+=1’ move the loop forward as much as len(word) however it seems like it doesn’t work as i supposed. Loop still move from last index of ‘i’.

Looks like a situation where the loop changes on each step and therefore i is simply taken from the iterator again. A fairly short example of this could be seen in the following-

for i in range(2):
    # on the next iteration i is taken from range (it's old value doesn't matter)
    print('value from iterator = {0}'.format(i))
    i += 10
    print('altered value = {0}'.format(i))

If this is the way you wanted to proceed then you’d need to step through the iterator itself. Use the built-in iter() function to get an iterator from range before your loop and use the built-in next() function inside the loop to step through that iterator. That might be a bit wordy so here’s an example

my_iterator = iter(range(10))
for x in my_iterator:
    print('{0} '.format(x), end = '')
    next(my_iterator)  # this would return the next element in the iterator
   # since we assign the return to nothing we simply step through the iterator
Out: 0 2 4 6 8
# Using next means we effectively ignore every other term (in this case, odd numbers)
# create the iterator with iter() prior to the loop