Censored confusion


#1



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):
    text=text.split
    cen='*'*len(word)
    
    for i in range(len(text)):
        if text[i]==word:
            text[i]=cen
        else:
            text[i]=text[i]
    return ' '.join(text)

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

def censor(text,word):
    cen='*'*len(word)
    s= text.split()

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

print censor(text,word)
'''


#2

Problem is caused by this line:

text=text.split

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

text = "Lorem ipsum dolor sit amet"

text = text.split

type(text)
# => <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(text)
# => <type 'list'>

#3

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

Thanks for clearing the issue up


#4

You're very welcome :slight_smile: