Function prints correct message but not working


#1

Can somebody explain, why the program doesn’t accept my code?

It goes as follows:

def censor (text, word):
  if word in text:
    my_text_split = text.split(" ")
    for i in range(0, len(my_text_split)):
      if my_text_split[i] == word:
        my_text_split[i] = "*" * len(word)
    text = " ".join(my_text_split)
    print text  
    
censor("hey hey hey", "hey")

The error message at the bottom says that the code doesn’t return the correct result for the string “hey hey hey”. Since it returns “*** *** ***” which should be correct I can’t quite grasp what’s wrong with the code

Thx for your help


How can I use split( ) to make censor?
#2

the error message is accurate, your function only prints the censored string and returns None (implicitly)


#3

Be sure that your return statement is not inside the if or the for.


#4

given print is at the correct spot, i don’t think this will be a problem for cj19954


#5

Is it though?

***
***
***

The return can’t go there since it would be inside the loop body.


#6

the print is fine in my case:

image

i literally copy pasted the code. Its just not returning.


#7

D’oh! I stand corrected. Text is joined already. What was I thinking? The return statement still stands, though.


#8

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