Censor (What is wrong with my code?)


#1



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


Did you create a function called censor? Your code threw a "global name 'censor' is not defined" error.

I did create a function!

def censor(text, word):
     new_string = " "
     new_string_array = []
     text = text.split()
     m = "*" * len(word) 
     for i in text:
         i == word:
             new_string_array.append(m)
     else:
         new_string_array.append(i)


#2

That line is not needed.

Missing the keyword, if.


#3

Ok, I tried this code:

def censor(text, word):
new_string_array = []
text = text.split()
m = "*" * len(word)
for i in text:
if i == word:
new_string_array.append(m)
else:
new_string_array.append(i)

It didn't work. Do I need to put a "return" statement?


#4

Yes, you will need to return your new_string_array, but as a string.


#5

I typed in this code:

def censor(text, word):
new_string_array = []
text = text.split()
m = "*" * len(word)
for i in text:
if i == word:
new_string_array.append(m)
return str(new_string_array)
else:
new_string_array.append(i)

However it said that my function fails on censor("hey hey hey","hey"). It returns "['']" when it should return " *** ***".


#6

Your loop needs to complete before any return.

The str function returns a single string, however it does not convert a list to a string, but to a list in quotes.

>>> str([1,2,3,4])
'[1, 2, 3, 4]'
>>>

#7

Do you mean use "break" , "continue" or "pass"?


#8

Neither of those. Let the loop iterate over the entire phrase (every word unit) before you write a return statement.


#9

But how do I do that?


#10

def censor(text, word):
    new_string_array = []
    text = text.split()
    m = "*" * len(word)
    for i in text:
        if i == word:
            new_string_array.append(m)
            return str(new_string_array)     # remove this line
        else:
            new_string_array.append(i)
    return ____    # return statement goes here

#11

I correct my mistake:

def censor(text, word):
new_string_array = []
text = text.split()
m = "*" * len(word)
for i in text:
if i == word:
new_string_array.append(m)
else:
new_string_array.append(i)
return str(new_string_array)

However It returns "['', '', '']" when it should return " *** ***". Should I had added a print statement?


#12

Recall the example from above...

>>> str([1,2,3,4])
'[1, 2, 3, 4]'
>>>

See how the function simply wraps the list in quotes? That is not the expected result. Consider reading up on the join method.


#13

def censor(text, word):
new_string_array = []
text = text.split()
m = "*" * len(word)
for i in word:
if == word:
print new_string_array.append(m)
else:
new_string_array.append(i)
return str.join(new_string_array)

I used the join method, but it came out of an invalid syntax on line 6.


#14
separator_string.join(list_or_string)

The separator string will be a space character,

" ".join(new_string_array)

#15

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.