[Help!] Oops, try again. Unfunctional Censor


#1


Hi all, I couldn't get pass this lesson, what's wrong with my code?

Here is the lesson:
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#

I got this error:

Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "['hey', 'hey', 'hey']" when it should return "*** *** ***".


def censor(text, word):
    lst = text.split()
    for chk in lst:
        if chk == word:
            chk = '*' * len(word)
    return ' '.join(lst)


#2

Hi arrayace,
I've solved it with the replace()-Function. If it was the suggested way of Learning I can't say, but it worked :slightly_smiling:

Greets
BSA


#4

you mean like this: ?

chk.replace('*' * len(word))


#5

We cannot change the iterator variable in a for construct. That is why we cannot mutate a list without an index iterator. Try using the index, instead.

def censor(text, word):
    lst = text.split()
    for k in range(len(lst)):
        if lst[k] == word:
            lst[k] = '*' * len(word)
    return ' '.join(lst)

#6

Thank you.
The iterator variable if For Loop can not be changed. I got that one.


#7

Consider, though, that this only applies to for k in iterable. When running an index we can change the variable, but we are playing with fire if not really careful and using rock solid logic in the process and reasoning.

Since Python is in the background running iter() and next(), we can fool it into believing that the index is anything we wish to make it. This is useful when using remove() or del(). We simply subtract one from the index and the next() in the background will increment that value. Again, I reiterate, this is not something to ever be treated lightly.


#8

Great, I've tried the same way but after several tries I've seen it didn't work I decided to made a wack "hack" like below :smile:

def censor(text,word):
return text.replace(word, ("*"*len(word)))

#9

That looks really neat.


#10

But is surely not the way it was mentioned... The way of mtf is the one wanted, you will see in the next lesson that there is a function, too, which could solve it, but we are adviced to do it the manual way, for exercise, and I agree, normally, cause...it was just a "wack ****" :slightly_smiling:


#11

We're not talking about built-in's, though, are we? This is algorithm practice. Good that you are looking up that side of Python, though. In production code the most refined is often the best, though not always. This is the catch-22 of programming and performance.


#12

I think so, too. It was just so alluring to solve it this particular time a way nobody learns nothing about coding. I've saved your solution for possible needed re-exercisings later, so thanks to both of you :wink:


#13

Off topic:

I am not a C programmer but it is only too evident that Python piggybacks on C, hence the plethora of modules, all or most are C. Because modules are compiled, only parts of our script need to be interpreted/compiled. This is where Python gets its speed.