Censored confusion


So I just finished the exercise #15.13.

After trying to hunt down an error Python was throwing up I decided to rewrite the whole thing. Which is the commented part in the code below, that works despite being a copy of the one above that throws up this error everytime its run

Traceback (most recent call last):
File "python", line 12, in
File "python", line 5, in censor
TypeError: object of type 'builtin_function_or_method' has no len()

The only difference I can spot is that the erroneous code has text=text.split() instead of s=text.spilt(). Which seems to be the cause of the error, my question now is why?
Shouldn't the initial text string get overwritten by the text=text.split() command and then behave like variable s in the the working code?

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

print censor("this is the strong string of strong", "strong")
text="this is the strong string of strong"

def censor(text,word):
    s= text.split()

    for i in range(len(s)):
        if s[i] == word:
            s[i] = cen
            s[i] == s[i]
    return ' '.join(s)

print censor(text,word)


Problem is caused by this line:


text.split is a method. And now text is... also a method.

text = "Lorem ipsum dolor sit amet"

text = text.split

# => <type 'builtin_function_or_method'>

What you need to do is to call this method to get list of the words, just like you did in the newer version:

text = "Lorem ipsum dolor sit amet"

text = text.split()

# => <type 'list'>


Always the missing parts, usually it's a colon though :slight_smile:

Thanks for clearing the issue up


You're very welcome :slight_smile: