Censor 10


Hi there

Why isnt this working?

File "python", line 7
i = "*" * len("word"))
SyntaxError: invalid syntax

def censor(text, word):
    liste = []
    print liste
    for i in liste:
        if i == word:
            i = "*" * len("word"))
    return liste        
    return " ".join(liste)
print censor("hello its me", "me")


This line is at the wrong place it should be after your for loop and if.


take a look at this line:

i = "*" * len("word"))

the number of opening and closing brackets don't match. That should resolve your current issue, if you need more help let us now, but first try it yourself


This line as well shouldn't be there.


additional print statements can be very useful to see what your code is doing, and won't cause a problem in this exercise.


under if i == word? and most importantly why??


thanks. still not working :slight_smile: its not censoring
[['hello', 'its', 'me']]
[['hello', 'its', 'me']]


Like i said, it doesn't resolve your issue completely. Maybe you should try first? It seems you split created a nested list (a list inside a list), maybe see what is causing it?


ahhh okay, thanks I will try to find it out by myself first


I am convinced you will learn more from trying it first yourself now, of course, if you are after a while are still stuck on this exercise, post an updated version of your code, and we will help you further :slight_smile:


What I meant was is not mandatory. The context in which I said was incorrect. :smile:


Now you are absolutely right :slight_smile: yes, it isn't mandatory but it can be useful while still building the program, you can remove them afterwards.


Yes, that is true. :grin:


ok Im still stuck. I know that I could use for in range but I dont understand why for i in liste will not work... I have a list and with that loop it should iterate through the list and replace the words?...or not?

def censor(text, word):
    liste = text.split()
    print liste
    for i in liste:
        if i == word:
            i = "*" * len("word")
    return " ".join(liste)
print censor("hello its me", "me")

(spaces are still the same as before)


and in range creates another list as far as I remember and then I would (again) have a nest as before ( list in list), how is that working?


Because i is just a local variable inside the loop (maybe even a copy), either way, you can't manipulate the list directly with for i in liste. Well, you can, but you need the build in replace function

Or you need to use range:

for i in range(len(liste)):

and then i is a number, so to get the text you would need text[i]. (replace all variable i inside the for loop to text[i]. Oh, and this:

"*" * len("word")

not wise it is to a string here, i would use the world variable since the length of it might be different

Censor (using Regular Expression module)

yes I already passed with in range... but I didnt understand why for i in would not work, but now I do! thanks!