Some help to write the right code - I don't want the solution


#1

Hey guys,

I am having some difficult to solve an exercise

The exercise: Censor

My code:

my_list = []
def censor(text, word):
  my_list = text.split()
  for i in my_list:
    new_word = ""
    if i == word:
      new_word = "*" * len(i)
      print new_word
      text.replace(i, new_word)
      print text
      return text

Thanks for any help


#2

Be sure your return line is not inside any other block than the function’s main block. Use four spaces for indentation, just to fit the norm around here.


Couple things worth noting…

This line could simply be,

new_word = "*" * len(word)

and remove the other line from inside the loop.

When done debugging, be sure to remove the print line.

text.replace()

is not an in-place operation. It needs to be assigned back on to the text variable.


#3

Thanks for replying, but I am still stuck here

My code:

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

#4

why is my_list defined outside the function? If we call the function twice now, the second function call will result in the censored result of both the first and second function call. That can’t be right

i will contain read-only values from the list, any changes made to it won’t persist

you join the list into a string, but do nothing with the result of this join action


#5

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