Censor 10


#1


Hi there

Why isnt this working?

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


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


#2

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


#3

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


#4

This line as well shouldn't be there.


#5

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


#6

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


#7

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


#9

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?


#10

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


#11

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:


#12

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


#13

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.


#14

Yes, that is true. :grin:


#15

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)


#16

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?


#17

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)
#18

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