Censor - why my loop wouldn't work?


#1

def censor(text,word):
for i in word:
new_text=""
if i in text:

        i="*"
    new_text+=i
return new_text

print censor("hey hey why","hey")

hello guys,

the code above was intended to be the basis loop for the Censor session. however, somehow the loop would not work. instead of replacing all Text Characters with "*", it would replace only the first one and won't show any other character, as could be seen from the copied screenshot. have no idea why is it. do you?




Replace this line with your code.


#2

Hello @bokery,

Can you please post your code and a link to the exercise.That would help greatly...


#3

thanks for commenting. it is my first post and it took me sometime to get it done. sorry.


#4

You should set your new_text variable outside of your for loop, since it is resetting every time it iterates over text...


#5

thanks for the fast feedback. just did it and instead of one "*" i received three "***". looks like it wouldn't run for the entire Text.


#6

here's the revised code


#7

You have to add the amount of asterisks as the length of the word, and also a space between the words' asterisks.


#8

thanks for the input. will see if i can solve it now,