Censor - what am I doing wrong?


#1

Could you please help me find out what is wrong with my code?

def censor(text, word):
    nb = len(word)
    bla = text.split()
    for i in range(len(bla)):
        if word == bla[i]:
            bla[i] = "*" * nb
    text = " ".join(bla)
    print text


Replace this line with your code.


#2

you should return text, not print it.


#3

It is the same issue when i replace "print" by "return"


#4

can i see an updated version of your code?


#5

def censor(text, word):
    nb = len(word)
    bla = text.split()
    for i in range(len(bla)):
        if word == bla[i]:
            bla[i] = "*" * nb
    text = " ".join(bla)
    return text

#6

the code is fine now, you should be able to pass the exercise. If not, you hit a glitch, try a different browser


#7

oh yeah! it is working now. so I was correct ? It was just something wrong with the wesbite? (just so i know)


#8

nope, the instructions state:
For example:

censor("this hack is wack hack", "hack")

should return

"this **** is wack ****"

see? The return is used in the instructions, clearly indicating that your function should return the censored string, so there is no problem with the exercise (yes, it doesn't accept print, but if the exercise wanted you to print, it would have asked for it)


#9

I see.
Could you please let me know why the code below which seems equivalent to the former one doe not work? Can't I change the content of what is in the "bla" list this way?

def censor(text, word):
    nb = len(word)
    bla = text.split()
    for words in bla:
        if word == words:
            words = "*" * nb
    text = " ".join(bla)
    return text

#10

no, if you use the for loop like this words gets assigned a copy of the values in the bla list. It doesn't give you access to the list, words gets re-assigned a new value (the next word in the bla list) every run of the loop, and cease to exist after the loop ends.

If you use range, you get numbers which you can use as indexes to manipulate the actually list.


10. censor - what is wrong with this code?
#11

super clear, thank you so much!